print attitude

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Batista, Facundo

    print attitude

    One detail I found, don't now if it's ok:

    Python 2.1.1 (#4, Mar 8 2002, 12:32:24)
    [GCC 2.95.3 20010315 (release)] on sunos5
    [color=blue][color=green][color=darkred]
    >>> a = 'ñ'
    >>> a[/color][/color][/color]
    '\xf1'[color=blue][color=green][color=darkred]
    >>> print a[/color][/color][/color]
    ñ[color=blue][color=green][color=darkred]
    >>> l = ['ñ']
    >>> l[/color][/color][/color]
    ['\xf1'][color=blue][color=green][color=darkred]
    >>> print l[/color][/color][/color]
    ['\xf1'][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Is this OK?

    Why this different behaviour?

    Thank you!

    .. Facundo


  • Erik Max Francis

    #2
    Re: print attitude

    "Batista, Facundo" wrote:
    [color=blue]
    > Is this OK?
    >
    > Why this different behaviour?[/color]

    It's str vs. repr, and unfortunately it generates no end of confusion.

    Let's make a simple object so we can clearly see the difference:
    [color=blue][color=green][color=darkred]
    >>> class Display:[/color][/color][/color]
    .... def __str__(self): return '(str)'
    .... def __repr__(self): return '(repr)'
    ....[color=blue][color=green][color=darkred]
    >>> d = Display()
    >>> str(d)[/color][/color][/color]
    '(str)'[color=blue][color=green][color=darkred]
    >>> repr(d)[/color][/color][/color]
    '(repr)'

    So whether the str or repr of the object is called, we can tell the
    difference. Now, consider:
    [color=blue][color=green][color=darkred]
    >>> print d[/color][/color][/color]
    (str)[color=blue][color=green][color=darkred]
    >>> d[/color][/color][/color]
    (repr)[color=blue][color=green][color=darkred]
    >>> [d][/color][/color][/color]
    [(repr)][color=blue][color=green][color=darkred]
    >>> print [d][/color][/color][/color]
    [(repr)]

    Printing an object prints its str, but simply specifying it as the value
    of an expression in the interactive interpreter prints the repr. What
    you're seeing is that both the str _and_ the repr of builtin container
    types print the _repr_ of their elements. I consider this something of
    a wart, but the rationalization is that it might be confusing if the str
    were used when using (say) list.__str__ since it might contain spaces or
    commas itself.
    [color=blue][color=green][color=darkred]
    >>> [' ', 'a, b', 'c', ' '][/color][/color][/color]
    [' ', 'a, b', 'c', ' ']

    If that were the str of the elements, it might be confusing:
    [color=blue][color=green][color=darkred]
    >>> '[%s]' % ', '.join(map(str, [' ', 'a, b', 'c', ' ']))[/color][/color][/color]
    '[ , a, b, c, ]'

    I see the reasoning, but I'm not sure the benefit of the decision
    outweighs the confusion it constantly calls. (This, along with standard
    floating point equality questions, are probably the most frequently
    asked questions about Python.)

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ But since when can wounded eyes see / If we weren't who we were
    \__/ Joi

    Comment

    • Donn Cave

      #3
      Re: print attitude

      In article <3F0669B5.679FB 505@alcyone.com >,
      Erik Max Francis <max@alcyone.co m> wrote:
      ....[color=blue]
      > Printing an object prints its str, but simply specifying it as the value
      > of an expression in the interactive interpreter prints the repr. What
      > you're seeing is that both the str _and_ the repr of builtin container
      > types print the _repr_ of their elements. I consider this something of
      > a wart, but the rationalization is that it might be confusing if the str
      > were used when using (say) list.__str__ since it might contain spaces or
      > commas itself.
      >[color=green][color=darkred]
      > >>> [' ', 'a, b', 'c', ' '][/color][/color]
      > [' ', 'a, b', 'c', ' ']
      >
      > If that were the str of the elements, it might be confusing:
      >[color=green][color=darkred]
      > >>> '[%s]' % ', '.join(map(str, [' ', 'a, b', 'c', ' ']))[/color][/color]
      > '[ , a, b, c, ]'
      >
      > I see the reasoning, but I'm not sure the benefit of the decision
      > outweighs the confusion it constantly calls. (This, along with standard
      > floating point equality questions, are probably the most frequently
      > asked questions about Python.)[/color]

      Speaking of floats, I believe lists containing floats are the main
      case where people really resist seeing the sense in this - but it's
      easier to call that a wart in float's repr, since they're as bugged
      wherever they see it and in my opinion rightly so.

      I think some time back, Steven Taschuk proposed to submit a rewrite
      of the str & repr documentation, and if that goes well it should be
      more or less explain the behavior of repr(list) without having to
      mention it specifically. Then that would make this kind of question
      an opportunity to clear up a whole area of confusion. Questions
      about a thing don't immediately indicate that it's ill conceived -
      I guess you have to consider how many questions there would be about
      the alternative.

      Donn Cave, donn@u.washingt on.edu

      Comment

      • Erik Max Francis

        #4
        Re: print attitude

        Donn Cave wrote:
        [color=blue]
        > Speaking of floats, I believe lists containing floats are the main
        > case where people really resist seeing the sense in this - but it's
        > easier to call that a wart in float's repr, since they're as bugged
        > wherever they see it and in my opinion rightly so.[/color]

        Personally, I think the internal difference between str and repr hits
        right upon a proper difference: str is for a "reasonable "
        human-readable representation, and repr is for as faithful and
        informative a representation as possible. These both have their uses
        and I approve of the distinction.

        The confusion, in my opinion, comes from the fortuity of when str vs.
        repr is used, which is easy to understand once you've been exposed to it
        but is very confusing at first. Even for someone who's familiar with
        the inaccuracy of floating point, you can very easily see how someone
        would be confused and worried about the following code fragment:
        [color=blue][color=green][color=darkred]
        >>> x = 1.4
        >>> print x[/color][/color][/color]
        1.4[color=blue][color=green][color=darkred]
        >>> print [x][/color][/color][/color]
        [1.3999999999999 999]

        --
        Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
        / \ We're here to preserve democracy, not to practice it.
        \__/ Capt. Frank Rasmey

        Comment

        • Donn Cave

          #5
          Re: print attitude

          In article <3F09E62C.D1A8D 53A@alcyone.com >,
          Erik Max Francis <max@alcyone.co m> wrote:
          [color=blue]
          > Donn Cave wrote:
          >[color=green]
          > > Speaking of floats, I believe lists containing floats are the main
          > > case where people really resist seeing the sense in this - but it's
          > > easier to call that a wart in float's repr, since they're as bugged
          > > wherever they see it and in my opinion rightly so.[/color]
          >
          > Personally, I think the internal difference between str and repr hits
          > right upon a proper difference: str is for a "reasonable "
          > human-readable representation, and repr is for as faithful and
          > informative a representation as possible. These both have their uses
          > and I approve of the distinction.
          >
          > The confusion, in my opinion, comes from the fortuity of when str vs.
          > repr is used, which is easy to understand once you've been exposed to it
          > but is very confusing at first. Even for someone who's familiar with
          > the inaccuracy of floating point, you can very easily see how someone
          > would be confused and worried about the following code fragment:
          >[color=green][color=darkred]
          > >>> x = 1.4
          > >>> print x[/color][/color]
          > 1.4[color=green][color=darkred]
          > >>> print [x][/color][/color]
          > [1.3999999999999 999][/color]

          Maybe it isn't time to wade back into this, only to repeat
          ad nauseum the same old discussion. My point is that there
          is a way to look at str vs. repr that 1) doesn't use utterly
          ambiguous phrases like "reasonable " or "human-readable", and
          2) makes it more or less self-evident that list will repr its
          contents. For more, see 61-message thread

          l3wpteil4jb0.dl g%4040tude.net& rnum=1&prev=/groups%3Fhl%3De n%26lr%3D%26ie%
          3DUTF-8%26selm%3Duw1i 4nqpl33h.1l3wpt eil4jb0.dlg%254 040tude.net

          Donn Cave, donn@u.washingt on.edu

          Comment

          • Donn Cave

            #6
            Re: print attitude

            In article <mailman.105770 1015.16570.pyth on-list@python.org >,
            Steven Taschuk <staschuk@telus planet.net> wrote:
            [color=blue]
            > Quoth Donn Cave:
            > [...][color=green]
            > > I think some time back, Steven Taschuk proposed to submit a rewrite
            > > of the str & repr documentation, [...][/color]
            >
            > I did. See
            > <http://www.python.org/sf/727789>
            > I am not entirely satisfied with the text (suggestions welcomed);
            > I haven't taken the time to look at the matter again.[/color]

            Well, I see I never did convince you to try to describe
            the intent of str directly.

            Donn Cave, donn@u.washingt on.edu

            Comment

            Working...