W
William Chang
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:
.... def __str__(self):
.... return "String Element"
....
But if overwrite __repr__:
.... def __repr__(self):
.... return "Repr Element"
....
Repr Element
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:
.... def __str__(self):
.... return "String Element"
....
String Elementa = [StrElement(), StrElement()]
print a<__main__.StrElement said:print StrElement()
But if overwrite __repr__:
.... def __repr__(self):
.... return "Repr Element"
....
[Repr Element, Repr Element]b = [ReprElement(), ReprElement()]
print b
Repr Element