Using __repr__ or __str__ for own printable class?

Ted Drain teddrain at earthlink.net
Tue Apr 22 12:10:42 EDT 2003


My $.02:

We use Python as a scripting interface to an underlying C++ system. 
The users write Python code to do what they want.  We ended up writing
__repr__ and __str__ in our classes to be the same thing because our
users were very annoyed when they typed:

>>> s
>>> print s

and got different results.

This doesn't necessarily mean that I'm not in favor of having the
distinction.  I think I'd rather see the behavior change when you do:
>>> s
To call __str__ instead of __repr__.  It seems to me that repr is a
specialized functionality that should be explicitly asked for when
needed.  __str__ is the more normal output and should be the default
in 'print s' or 's'.

Ted   teddrain at earthlink.net




More information about the Python-list mailing list