No subject
Sun Nov 12 08:01:13 CET 2006
__repr__ (self)
Called by the repr() built-in function and by string
conversions (reverse quotes) to compute the ``official'' string
representation of an object. This should normally look like a
valid Python expression that can be used to recreate an object
with the same value. By convention, objects which cannot be
trivially converted to strings which can be used to create a
similar object produce a string of the form "<...some useful
description...>".
__str__ (self)
Called by the str() built-in function and by the print
statement to compute the ``informal'' string representation of
an object. This differs from __repr__() in that it does not
have to be a valid Python expression: a more convenient or
concise representation may be used instead.
The idea is that
eval(repr(x)) == x
should be true as much as is possible, whereas
str(x)
should be fit for human consumption.
> > 2) Or, whatever they return, should they return the same value?
It's worth noting that the *only* type I know of that has a different
`str' to its `repr' is the string type:
>>> print str("hello")
hello
>>> print repr("hello")
'hello'
Regards,
Michael
More information about the Python-list
mailing list