__str__ vs. __repr__

Cliff Crawford cjc26 at nospam.cornell.edu
Mon Nov 8 02:29:34 CET 1999

I'm glad this thread cropped up when it did.  I've been writing a
simple parser module, and had been trying to track down a "bug"
involving the token scanner inserting extra backslashes in strings.
But then I thought of the __repr__ vs __str__ distinction, and
tried this:

>>> r'blah\\blah'
>>> 'blah\\blah'
>>> print r'blah\\blah'
>>> print 'blah\\blah'

So actually there wasn't anything wrong with my code, it was just
that I was printing my results in the interpreter, which uses
__repr__ and thus inserts an extra backslash for each backslash in
the string.
Well, it only cost me about a half hour of needless debugging =)

cliff crawford   http://www.people.cornell.edu/pages/cjc26/
-><-                        Shall she hear the lion's roar?

More information about the Python-list mailing list