Re: confused about __str__ vs. __repr__

Discussion in 'Python' started by Neal Becker, Dec 18, 2008.

  1. Neal Becker

    Neal Becker Guest

    Tino Wildenhain wrote:

    > Neal Becker wrote:
    >> Reading some FAQ, I see that __str__ is "meant for human eyes".
    >>
    >> But it seems that:
    >> class X(object):
    >> def __str__(self):
    >> return "str"
    >> def __repr__(self):
    >> return "repr"
    >>
    >> x = X()
    >> d = {0 : x}
    >> print d
    >> {0: repr}
    >>
    >> So if __str__ is "meant for human eyes", then why isn't print using it!

    >
    > it is:
    >
    > > print x

    > str
    >
    > but dict just uses repr() for all its childs to print.
    >
    > T.

    That makes no sense to me. If I call 'print' on a container, why wouldn't it recursively print on the contained objects? Since print means call str, printing a container should recursively call str on the objects.
     
    Neal Becker, Dec 18, 2008
    #1
    1. Advertising

  2. Neal Becker

    Mel Guest

    Neal Becker wrote:

    > Tino Wildenhain wrote:
    >
    >> Neal Becker wrote:
    >>> Reading some FAQ, I see that __str__ is "meant for human eyes".
    >>>
    >>> But it seems that:
    >>> class X(object):
    >>> def __str__(self):
    >>> return "str"
    >>> def __repr__(self):
    >>> return "repr"
    >>>
    >>> x = X()
    >>> d = {0 : x}
    >>> print d
    >>> {0: repr}
    >>>
    >>> So if __str__ is "meant for human eyes", then why isn't print using it!

    >>
    >> it is:
    >>
    >> > print x

    >> str
    >>
    >> but dict just uses repr() for all its childs to print.
    >>
    >> T.

    > That makes no sense to me. If I call 'print' on a container, why wouldn't
    > it recursively print on the contained objects? Since print means call
    > str, printing a container should recursively call str on the objects.


    Basically because there are too many right ways to format the resulting
    report. Space separated? Tab separated? One per line? Boxes around
    them? As HTML definition lists? Creating a standard report form would
    take a lot of work and wouldn't, finally, solve very many peoples'
    problems.

    Mel.
     
    Mel, Dec 18, 2008
    #2
    1. Advertising

  3. Neal Becker

    Neal Becker Guest

    Mel wrote:

    > Neal Becker wrote:
    >
    >> Tino Wildenhain wrote:
    >>
    >>> Neal Becker wrote:
    >>>> Reading some FAQ, I see that __str__ is "meant for human eyes".
    >>>>
    >>>> But it seems that:
    >>>> class X(object):
    >>>> def __str__(self):
    >>>> return "str"
    >>>> def __repr__(self):
    >>>> return "repr"
    >>>>
    >>>> x = X()
    >>>> d = {0 : x}
    >>>> print d
    >>>> {0: repr}
    >>>>
    >>>> So if __str__ is "meant for human eyes", then why isn't print using it!
    >>>
    >>> it is:
    >>>
    >>> > print x
    >>> str
    >>>
    >>> but dict just uses repr() for all its childs to print.
    >>>
    >>> T.

    >> That makes no sense to me. If I call 'print' on a container, why
    >> wouldn't
    >> it recursively print on the contained objects? Since print means call
    >> str, printing a container should recursively call str on the objects.

    >
    > Basically because there are too many right ways to format the resulting
    > report. Space separated? Tab separated? One per line? Boxes around
    > them? As HTML definition lists? Creating a standard report form would
    > take a lot of work and wouldn't, finally, solve very many peoples'
    > problems.
    >
    > Mel.
    >

    Thanks, but the question of how to format the container is different from how to format the primitive elements of the container. I was suggesting that printing an element of a container should be consistent with printing the element without the container, i.e.,

    print [a]
    should be consistent with
    print a
     
    Neal Becker, Dec 18, 2008
    #3
  4. Neal Becker

    Steve Holden Guest

    Neal Becker wrote:
    > Mel wrote:
    >
    >> Neal Becker wrote:
    >>
    >>> Tino Wildenhain wrote:
    >>>
    >>>> Neal Becker wrote:
    >>>>> Reading some FAQ, I see that __str__ is "meant for human eyes".
    >>>>>
    >>>>> But it seems that:
    >>>>> class X(object):
    >>>>> def __str__(self):
    >>>>> return "str"
    >>>>> def __repr__(self):
    >>>>> return "repr"
    >>>>>
    >>>>> x = X()
    >>>>> d = {0 : x}
    >>>>> print d
    >>>>> {0: repr}
    >>>>>
    >>>>> So if __str__ is "meant for human eyes", then why isn't print using it!
    >>>> it is:
    >>>>
    >>>> > print x
    >>>> str
    >>>>
    >>>> but dict just uses repr() for all its childs to print.
    >>>>
    >>>> T.
    >>> That makes no sense to me. If I call 'print' on a container, why
    >>> wouldn't
    >>> it recursively print on the contained objects? Since print means call
    >>> str, printing a container should recursively call str on the objects.

    >> Basically because there are too many right ways to format the resulting
    >> report. Space separated? Tab separated? One per line? Boxes around
    >> them? As HTML definition lists? Creating a standard report form would
    >> take a lot of work and wouldn't, finally, solve very many peoples'
    >> problems.
    >>
    >> Mel.
    >>

    > Thanks, but the question of how to format the container is different from how to format the primitive elements of the container. I was suggesting that printing an element of a container should be consistent with printing the element without the container, i.e.,
    >
    > print [a]
    > should be consistent with
    > print a
    >

    Indeed, but that only demonstrates that you have failed to take the
    point of the more perspicacious replied to your query.

    Python is trying to avoid throwing away useful information. The only
    people who would want to see a representation of a data structure such
    as a list are programmers: if you want to show a list of values to a
    regular user you format it in some required way.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Dec 18, 2008
    #4
    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.
Similar Threads
  1. Jim Newton

    how to use __str__ and __repr__?

    Jim Newton, Jun 7, 2004, in forum: Python
    Replies:
    15
    Views:
    560
    Peter Maas
    Jun 9, 2004
  2. Dan Sommers
    Replies:
    3
    Views:
    307
    Serge Orlov
    Feb 19, 2005
  3. Jan Danielsson

    FAQ: __str__ vs __repr__

    Jan Danielsson, Jun 15, 2005, in forum: Python
    Replies:
    15
    Views:
    742
    Skip Montanaro
    Jun 21, 2005
  4. Neal Becker

    confused about __str__ vs. __repr__

    Neal Becker, Dec 18, 2008, in forum: Python
    Replies:
    1
    Views:
    257
    Diez B. Roggisch
    Dec 18, 2008
  5. Neal Becker

    Re: confused about __str__ vs. __repr__

    Neal Becker, Dec 18, 2008, in forum: Python
    Replies:
    12
    Views:
    629
    Carl Banks
    Dec 19, 2008
Loading...

Share This Page