how to use __str__ and __repr__?

Erik Max Francis max at alcyone.com
Tue Jun 8 05:02:24 EDT 2004


Jim Newton wrote:

> if that is the case then why does the following fail
> 
> class another:
>         pass
> 
> another.__str__()
> 
> I would think that would return a string such as
> "<__main__.another instance at 0x8132b64>"
> but it does not seem to.

Because __str__ is a special method which str defers to.  If you want
the str of anObject, call str(anObject).  The object will then show you
the string representation for that object.  You shouldn't care whether
or not it's got that behavior from defining a __str__ method, or whether
it's gotten it from a parent class.

-- 
 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ To endure what is unendurable is true endurance.
    -- (a Japanese proverb)



More information about the Python-list mailing list