__repr__ vs. __str__

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.


import datetime

Create A Simple Object

class Regiment(object):

    def __init__(self, date=datetime.datetime.now()):
        self.date = date

    def __repr__(self):
        return date

    def __str__(self):
        return str(date)

__repr__ is for the developer. It is string representation of the object and the code needed to reproduce the object.

__str__ is the output for the end user. It prints what the user wants to see.