v repr vs. str - Python

repr vs. str

Interesting in learning more? Check out Fluent Python

Preliminaries

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.