470,849 Members | 1,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Repr or Str ?

Where and when is good/nescessary to use `repr` instead of `str` ?
Can you please explain the differences
Thanks
LL

Feb 6 '07 #1
5 2500
On Feb 6, 11:47 am, "Johny" <pyt...@hope.czwrote:
Where and when is good/nescessary to use `repr` instead of `str` ?
Can you please explain the differences
Thanks
RTFM. 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 at all possible, this should look like a valid Python
expression that could be used to recreate an object with the same
value (given an appropriate environment). If this is not possible, a
string of the form "<...some useful description...>" should be
returned. The return value must be a string 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.

This is typically used for debugging, so it is important that the
representation is information-rich and unambiguous.

__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 return value must be a string object.
Feb 6 '07 #2

str is a text representation of the object, you can see it as a nice print
repr is the text representation of the object that you can evaluate to
get the same object

Johny wrote:
Where and when is good/nescessary to use `repr` instead of `str` ?
Can you please explain the differences
Thanks
LL

Feb 6 '07 #3
"Johny" <py****@hope.czwrote:
Where and when is good/nescessary to use `repr` instead of `str` ?
Can you please explain the differences
roughly, repr() is for programmers, str() is for end-users.

</F>

Feb 6 '07 #4
On Feb 6, 5:47 am, "Johny" <pyt...@hope.czwrote:
Where and when is good/nescessary to use `repr` instead of `str` ?
Can you please explain the differences
Thanks
LL
When you want to provide a representation of an object from which you
can create another object if you had to.
Use 'str' if you just want to print it nicely.

Feb 6 '07 #5
In article <11*********************@m58g2000cwm.googlegroups. com>,
"Johny" <py****@hope.czwrote:
Where and when is good/nescessary to use `repr` instead of `str` ?
Can you please explain the differences
You expect repr to include information that you might call
`meta-data' or `type' -- object class and so forth. To the
extent that this is of any interest, it's more or less equally
of interest with all objects.

If you go to the trouble to support str separately, it's a data
conversion and of course should render only the data. An application
should be able to use str() to force data to string type (that's
what I mean by conversion.) If the object can't sensibly be
converted to string type, then normally __str__ is omitted, and
defaults to __repr__.

Donn Cave, do**@u.washington.edu
Feb 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by tcronj | last post: by
1 post views Thread by Edward C. Jones | last post: by
1 post views Thread by Noam Raphael | last post: by
5 posts views Thread by nummertolv | last post: by
2 posts views Thread by slowness.chen | last post: by
9 posts views Thread by Matthew Wilson | last post: by
5 posts views Thread by Leif K-Brooks | last post: by
1 post views Thread by Raymond Hettinger | last post: by
reply views Thread by Terry Reedy | last post: by
2 posts views Thread by Mark Tolonen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.