Repr or Str ?

J

Johny

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

Bart Ogryczak

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.
 
O

Olivier Feys

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
 
F

Fredrik Lundh

Johny said:
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>
 
N

Nick Vatamaniuc

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.
 
D

Donn Cave

"Johny said:
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, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top