confused about __str__ vs. __repr__
mikael at isy.liu.se
Thu Dec 18 17:05:32 CET 2008
Diez B. Roggisch wrote:
> Yep. And it's easy enough if you don't care about them being different..
> def __repr__(self):
> return str(self)
If I ever wanted __str__ and __repr__ to return the same thing, I would
make them equal:
return 'whatever you want'
__repr__ = __str__
That makes it more obvious to me what's going on. As a bonus, it saves
one method call for every repr call.
More information about the Python-list