how to use __str__ and __repr__?

Jim Newton jimka at rdrop.com
Mon Jun 7 17:53:34 EDT 2004


i read that in the documenation. and i assumed from that that
     print another()
actually prints the string returned from another().__str__()
and thus __str__ must be being inherited from the superclass
of another, but apparently print does something different.

why does print another() actually print something rather than
complaining that there is no __str__ defined?

-jim


Peter Hansen wrote:
> Jim Newton wrote:
> 
>> hi all, does anyone know what print does if there is no __str__ method?
> 
> 
>  From the documentation at http://docs.python.org/ref/customization.html :
> 
> __repr__( self)
> 
> Called by the repr() built-in function and by string conversions 
> (reverse quotes) to compute the ``official'' string representation of an 
> object. .... If a class defines __repr__() but not __str__(), then 
> __repr__() is also used when an ``informal'' string representation of 
> instances of that class is required.
> 
> Does that help?




More information about the Python-list mailing list