how to use __str__ and __repr__?

Peter Hansen peter at engcorp.com
Mon Jun 7 18:25:13 EDT 2004


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