confused about __str__ vs. __repr__

Mikael Olofsson 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:

def __str__(self):
     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.

/MiO



More information about the Python-list mailing list