Repr or Str ?

Discussion in 'Python' started by Johny, Feb 6, 2007.

  1. Johny

    Johny Guest

    Where and when is good/nescessary to use `repr` instead of `str` ?
    Can you please explain the differences
    Thanks
    LL
    Johny, Feb 6, 2007
    #1
    1. Advertising

  2. On Feb 6, 11:47 am, "Johny" <> 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.
    Bart Ogryczak, Feb 6, 2007
    #2
    1. Advertising

  3. Johny

    Olivier Feys Guest

    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
    >
    >
    Olivier Feys, Feb 6, 2007
    #3
  4. "Johny" <> 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>
    Fredrik Lundh, Feb 6, 2007
    #4
  5. On Feb 6, 5:47 am, "Johny" <> 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.
    Nick Vatamaniuc, Feb 6, 2007
    #5
  6. Johny

    Donn Cave Guest

    In article <>,
    "Johny" <> 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,
    Donn Cave, Feb 6, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page