Using __repr__ or __str__ for own printable class?

Terry Reedy tjreedy at udel.edu
Tue Apr 22 13:08:49 EDT 2003


"Ted Drain" <teddrain at earthlink.net> wrote in message
news:f6a31cb8.0304220810.547f5acd at posting.google.com...
> 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.

If you think of repr(o) as the codewriter view of an object o and
str(o) as the (passive) user view of o, then printing repr(o) in
interactive (codewriter) mode makes perfect sense.

There can, of course, be conflicts when users write code while still
thinking like users ;-)

Terry J. Reedy






More information about the Python-list mailing list