printing a sequence...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Florian Preknya

    printing a sequence...

    Hi everybody,

    I'm relatively new in using Python and I want to perform a simple job: I
    have a list of objects of type A and want to print them to stdout.

    class A:
    def __init__(self, value):
    self.value = value

    def __str__(self):
    return "value: %d" % self.value

    a = [A(10), A(12), A(2)]
    print str(a)

    I obtain [<__main__.A instance at 0x00767D80>, <__main__.A instance at
    0x00768698>, <__main__.A instance at 0x00768350>].
    What I want to obtain is [value: 10, value: 12, value: 2].

    I know that if I change the __str__ to __repr__ I obtain what I want, but
    then I will have these informal strings in debuger also, and I do not want
    that.

    Can I write somehow the print statement to force calling the A.__str__
    function?

    Thanks,
    Florian.


  • Peter Otten

    #2
    Re: printing a sequence...

    Florian Preknya wrote:
    [color=blue]
    > Hi everybody,
    >
    > I'm relatively new in using Python and I want to perform a simple job: I
    > have a list of objects of type A and want to print them to stdout.
    >
    > class A:
    > def __init__(self, value):
    > self.value = value
    >
    > def __str__(self):
    > return "value: %d" % self.value
    >
    > a = [A(10), A(12), A(2)]
    > print str(a)
    >
    > I obtain [<__main__.A instance at 0x00767D80>, <__main__.A instance at
    > 0x00768698>, <__main__.A instance at 0x00768350>].
    > What I want to obtain is [value: 10, value: 12, value: 2].
    >
    > I know that if I change the __str__ to __repr__ I obtain what I want, but
    > then I will have these informal strings in debuger also, and I do not want
    > that.
    >
    > Can I write somehow the print statement to force calling the A.__str__
    > function?[/color]

    print map(str, a)

    or

    print [str(item) for item in a]

    Peter

    Comment

    • Peter Otten

      #3
      Re: printing a sequence...

      Peter Otten wrote:
      [color=blue]
      > Florian Preknya wrote:[/color]
      [color=blue][color=green]
      >> What I want to obtain is [value: 10, value: 12, value: 2].[/color][/color]
      [color=blue][color=green]
      >> Can I write somehow the print statement to force calling the A.__str__
      >> function?[/color]
      >
      > print map(str, a)
      >
      > or
      >
      > print [str(item) for item in a][/color]

      Oops, the above will still print the representation of the strings. When you
      don't want quotes and escaped non-ascii characters, use

      print "[%s]" % ", ".join(map( str, a))

      Peter


      Comment

      • Christopher T King

        #4
        Re: printing a sequence...

        On Fri, 16 Jul 2004, Peter Otten wrote:
        [color=blue]
        > Peter Otten wrote:
        >[color=green]
        > > Florian Preknya wrote:[/color]
        >[color=green][color=darkred]
        > >> What I want to obtain is [value: 10, value: 12, value: 2].[/color][/color]
        >[color=green][color=darkred]
        > >> Can I write somehow the print statement to force calling the A.__str__
        > >> function?[/color]
        > >
        > > print [str(item) for item in a][/color]
        >
        > print "[%s]" % ", ".join(map( str, a))[/color]

        I had the same problem with the same solution, and it makes me wonder, why
        does a list's __str__() method call the __repr__() method of its members?
        Personally, I think the __str__() call should be propagated, not turned
        into a __repr__() call. I can't think of any instances in which such
        behavior is what you want (if the __repr__() of the members is wanted,
        then __repr__() should be used).

        Comment

        • Peter Otten

          #5
          Re: printing a sequence...

          Christopher T King wrote:
          [color=blue]
          > On Fri, 16 Jul 2004, Peter Otten wrote:
          >[color=green]
          >> Peter Otten wrote:
          >>[color=darkred]
          >> > Florian Preknya wrote:[/color]
          >>[color=darkred]
          >> >> What I want to obtain is [value: 10, value: 12, value: 2].[/color]
          >>[color=darkred]
          >> >> Can I write somehow the print statement to force calling the A.__str__
          >> >> function?
          >> >
          >> > print [str(item) for item in a][/color]
          >>
          >> print "[%s]" % ", ".join(map( str, a))[/color]
          >
          > I had the same problem with the same solution, and it makes me wonder, why
          > does a list's __str__() method call the __repr__() method of its members?
          > Personally, I think the __str__() call should be propagated, not turned
          > into a __repr__() call. I can't think of any instances in which such
          > behavior is what you want (if the __repr__() of the members is wanted,
          > then __repr__() should be used).[/color]

          I didn't find it in the FAQ; I think the argument goes that the results of
          propagating str() are sometimes misleading, e. g:
          [color=blue][color=green][color=darkred]
          >>> class List(list):[/color][/color][/color]
          .... def __str__(self):
          .... return "[%s]" % ", ".join(map( str, self))
          ....[color=blue][color=green][color=darkred]
          >>> List(["a", "b, c", "d"])[/color][/color][/color]
          ['a', 'b, c', 'd'][color=blue][color=green][color=darkred]
          >>> print List(["a", "b, c", "d"])[/color][/color][/color]
          [a, b, c, d][color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          OK, there may be subtler examples. Personally, I would prefer the above
          problem over the current inconsistency.

          While I'm at it, here's another alternative for the OP that handles nested
          lists nicely:
          [color=blue][color=green][color=darkred]
          >>> class D:[/color][/color][/color]
          .... def __init__(self, value):
          .... self.value = value
          .... def __repr__(self):
          .... if isinstance(self .value, list):
          .... return str(map(D, self.value))
          .... else:
          .... return str(self.value)
          ....[color=blue][color=green][color=darkred]
          >>> print D(["a", "b", "c"])[/color][/color][/color]
          [a, b, c][color=blue][color=green][color=darkred]
          >>> print D(["a", "b", ["c", "d"]])[/color][/color][/color]
          [a, b, [c, d]][color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          Peter

          Comment

          • Lonnie Princehouse

            #6
            Re: printing a sequence...

            To clarify what's already been said-

            thing.__str__() is equivalent to str(thing)

            thing.__repr__( ) is equivalent to repr(thing)


            str(some_list) will call repr() for all of the list's items.

            You could try:

            print map(str, a)

            or

            print [str(x) for x in a]

            for the desired effect, or you could overload __repr__ instead of __str__


            "Florian Preknya" <bolo@coco.ro > wrote in message news:<cd8a1u$t6 d$1@nebula.dntt m.ro>...[color=blue]
            > Hi everybody,
            >
            > I'm relatively new in using Python and I want to perform a simple job: I
            > have a list of objects of type A and want to print them to stdout.
            >
            > class A:
            > def __init__(self, value):
            > self.value = value
            >
            > def __str__(self):
            > return "value: %d" % self.value
            >
            > a = [A(10), A(12), A(2)]
            > print str(a)
            >
            > I obtain [<__main__.A instance at 0x00767D80>, <__main__.A instance at
            > 0x00768698>, <__main__.A instance at 0x00768350>].
            > What I want to obtain is [value: 10, value: 12, value: 2].
            >
            > I know that if I change the __str__ to __repr__ I obtain what I want, but
            > then I will have these informal strings in debuger also, and I do not want
            > that.
            >
            > Can I write somehow the print statement to force calling the A.__str__
            > function?
            >
            > Thanks,
            > Florian.[/color]

            Comment

            • Peter Otten

              #7
              Re: printing a sequence...

              Lonnie Princehouse wrote:
              [color=blue]
              > print [str(x) for x in a][/color]

              will call repr(str(x)) for all x with undesirable effects for non-ascii
              characters and extra quoting.

              Looks like you made the same error that I did - after I corrected it :(

              Peter




              Comment

              • Dan Bishop

                #8
                Re: printing a sequence...

                Christopher T King <squirrel@WPI.E DU> wrote in message news:<Pine.LNX. 4.44.0407161004 150.10067-100000@ccc8.wpi .edu>...[color=blue]
                > On Fri, 16 Jul 2004, Peter Otten wrote:
                >[color=green]
                > > Peter Otten wrote:
                > >[color=darkred]
                > > > Florian Preknya wrote:[/color][/color]
                >[color=green][color=darkred]
                > > >> What I want to obtain is [value: 10, value: 12, value: 2].[/color][/color]
                >[color=green][color=darkred]
                > > >> Can I write somehow the print statement to force calling the A.__str__
                > > >> function?
                > > >
                > > > print [str(item) for item in a][/color]
                > >
                > > print "[%s]" % ", ".join(map( str, a))[/color]
                >
                > I had the same problem with the same solution, and it makes me wonder, why
                > does a list's __str__() method call the __repr__() method of its members?
                > Personally, I think the __str__() call should be propagated, not turned
                > into a __repr__() call. I can't think of any instances in which such
                > behavior is what you want (if the __repr__() of the members is wanted,
                > then __repr__() should be used).[/color]

                seq = ['A, B', 'C']
                print seq

                Comment

                Working...