Check out the Python documentation at: http://docs.python.org/reference/datamodel.html#object. repr
an object. magnesia (self)
Called by the repr() built-in function and by string conversions
(backticks) to evaluate the "official" string representation of an object. If at all possible, it should look like a valid Python expression that can be used to recreate an object with the same value (given the appropriate environment). If this is not possible, the form string <... some useful description ...> should be returned. The return value must be a string. If the class defines repr () but not str (), then repr () is also used when an โInformalโ string representation of instances of this class is required.
This is typically used for debugging, so it is important that the
Representationis informational and unambiguous.
an object. st (self)
Called by the str() built-in function and by the print statement
to compute an "informal" string representation of an object. This differs from repr () in that it should not be a valid Python expression: a more convenient or compressed representation may be used instead. The return value must be a string object.
Example:
>>> class A(): ... def __repr__(self): return "repr!" ... def __str__(self): return "str!" ... >>> a = A() >>> a repr! >>> print(a) str! >>> class B(): ... def __repr__(self): return "repr!" ... >>> class C(): ... def __str__(self): return "str!" ... >>> b = B() >>> b repr! >>> print(b) repr! >>> c = C() >>> c <__main__.C object at 0x7f7162efb590> >>> print(c) str!
The print function prints all __str__ arguments to the console. Like print(str(obj)) .
But in the interactive console, return the value of the __repr__ function. And if __str__ not defined, you can use __repr__ .
Ideally, __repr__ means we should just use this view to play this object. It should not be identical between different classes or an object representing different values. For example, datetime.time:
But __str__ (what we get from str(obj) ) should seem nice, because we are showing it to the user.
>>> a = datetime.time(16, 42, 3) >>> repr(a) 'datetime.time(16, 42, 3)' >>> str(a) '16:42:03'
And, sorry for the bad English :).