Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Repr or Str ?

Reply
Thread Tools

Repr or Str ?

 
 
Johny
Guest
Posts: n/a
 
      02-06-2007
Where and when is good/nescessary to use `repr` instead of `str` ?
Can you please explain the differences
Thanks
LL

 
Reply With Quote
 
 
 
 
Bart Ogryczak
Guest
Posts: n/a
 
      02-06-2007
On Feb 6, 11:47 am, "Johny" <(E-Mail Removed)> wrote:
> 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.


 
Reply With Quote
 
 
 
 
Olivier Feys
Guest
Posts: n/a
 
      02-06-2007

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


 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      02-06-2007
"Johny" <(E-Mail Removed)> wrote:
> 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>



 
Reply With Quote
 
Nick Vatamaniuc
Guest
Posts: n/a
 
      02-06-2007
On Feb 6, 5:47 am, "Johny" <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      02-06-2007
In article <(E-Mail Removed). com>,
"Johny" <(E-Mail Removed)> wrote:

> 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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
str() and repr() question adima Python 2 04-26-2007 02:05 PM
print / str / repr Nikolai Kirsebom Python 1 05-18-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM
str() or repr() tcronj Python 0 02-12-2004 06:14 PM
what's the deference between str=null and str=" " ???????? David Java 2 08-03-2003 04:10 PM



Advertisments