printing list, is this a bug?
William Chang
mr.williamchang at gmail.com
Fri May 25 16:55:24 EDT 2007
Is the different behavior between __repr__ and __str__ intentional
when it comes to printing lists? Basically I want to print out a list
with elements of my own class, but when I overwrite __str__, __str__
doesn't get called but if I overwrite __repr__, __repr__ will get
called. Is this a bug?
For example:
>>> class StrElement(object):
... def __str__(self):
... return "String Element"
...
>>> a = [StrElement(), StrElement()]
>>> print a
[<__main__.StrElement object at 0xb7dc05cc>, <__main__.StrElement
object at 0xb7dc048c>]
>>> print StrElement()
String Element
But if overwrite __repr__:
>>> class ReprElement(object):
... def __repr__(self):
... return "Repr Element"
...
>>> b = [ReprElement(), ReprElement()]
>>> print b
[Repr Element, Repr Element]
>>> print ReprElement()
Repr Element
More information about the Python-list
mailing list