Entry
What is the difference between __repr__() and __str__()?
What is __repr__ used for?
What is __str__ used for?
Oct 17th, 2009 06:13
adel85 adel85, Nathan Wallace, unknown unknown, Gregoire Welraeds, Fredrik Lundh
according to the eff-bot:
__repr__ should something that makes sense to a
programmer, but not necessarily to someone else.
(think "debugging")
(e.g. basic types return a string literal, more complex
types usually return some kind of descriptor, such as
"<foo instance at 912388>" or "<open file 'foo', mode
'r' at 918747>")
__str__ should be used to convert the contents of an
object to a (usually printable) string, whenever that
makes sense (think "class design")
According to the Python reference manual, page 17
__repr__: called to compute the official string representation of
an
object. This should like a valid Python expression that can be
used
to recreate an object with the same value.
__str__: differs from repr in that in does not have to be a valid
python expression: a more conveniant or concise representation
maybe
used instead.
http://www.borsaat.com/vb/f2/
http://www.borsaat.com/vb/f4/
http://www.borsaat.com/vb/f8/
http://www.borsaat.com/vb/f14/
http://www.borsaat.com/vb/f28/
http://www.borsaat.com/vb/f30/
http://www.borsaat.com/vb/f10/
http://www.borsaat.com/vb/f11/
http://www.borsaat.com/vb/f12/
http://www.borsaat.com/vb/f13/
http://www.borsaat.com/vb/f6/
http://www.borsaat.com/vb/f64/
http://www.borsaat.com/vb/f38/
http://www.borsaat.com/vb/f41/
http://www.borsaat.com/vb/f44/
http://www.borsaat.com/vb/f56/
http://www.borsaat.com/vb/f45/
http://www.borsaat.com/vb/f65/
http://www.borsaat.com/vb/f57/
http://www.borsaat.com/vb/f23/
http://www.borsaat.com/vb/f54/
http://www.borsaat.com/vb/f22/
http://www.borsaat.com/vb/f55/
http://www.borsaat.com/vb/f43/
http://www.borsaat.com/vb/f66/
http://www.borsaat.com/vb/f67/
http://www.borsaat.com/vb/f69/
http://www.borsaat.com/vb/f42/
http://www.borsaat.com/vb/f68/
http://www.borsaat.com/vb/f70/
http://www.borsaat.com/vb/f26/
http://www.borsaat.com/vb/
http://www.borsaat.com/vb/f90/