FAQ: __str__ vs __repr__

Skip Montanaro skip at pobox.com
Tue Jun 21 15:44:52 CEST 2005

    >> __repr__ shouldn't be anything, if you don't have an actual need for
    >> it.  Neither should __str__.

    Simon> Oh, I don't know. __str__ is so frequently useful in debugging
    Simon> and logging that I always try and do something useful with it.

And sometimes __repr__ inherited from a base class doesn't tell you much.
If you inherit from gobject (the basic object in PyGtk), the repr is
something like

    <Future object (__main__+Base) at 0x851384c>

That is, it identifies the class name, its inheritance hierarchy (Future ->
Base -> __main__ in this case) and its memory address.  That's perhaps
useful by itself in some contexts, and I can understand that the PyGtk folks
couldn't really stuff more specific info in there, but it does nothing to
distinguish one instance's state from that of another.


More information about the Python-list mailing list