Recursive Str?

Discussion in 'Python' started by Chris S., Oct 22, 2004.

  1. Chris S.

    Chris S. Guest

    Why do most, if not all, compound Python structures not convert their
    elements to strings recursively?

    >>> class foo:

    .... def __init__(self, a):
    .... self.a = a
    .... def __str__(self):
    .... return 'foo('+str(self.a)+')'
    ....
    >>> from sets import Set
    >>> a=Set([foo(1)])
    >>> print a

    Set([<__main__.foo instance at 0x01C01968>])
    >>> def SetStr(self):

    .... text = []
    .... for n in self:
    .... text.append(str(n))
    .... return 'Set('+', '.join(text)+')'
    ....
    >>> Set.__str__ = SetStr
    >>> print a

    Set(foo(1))
     
    Chris S., Oct 22, 2004
    #1
    1. Advertising

  2. Chris S. <chrisks <at> NOSPAM.udel.edu> writes:

    >
    > Why do most, if not all, compound Python structures not convert their
    > elements to strings recursively?
    >
    > >>> class foo:

    > ... def __init__(self, a):
    > ... self.a = a
    > ... def __str__(self):
    > ... return 'foo('+str(self.a)+')'
    > ...
    > >>> from sets import Set
    > >>> a=Set([foo(1)])
    > >>> print a

    > Set([<__main__.foo instance at 0x01C01968>])


    Well, they call the __repr__ method, not the __str__ method:

    >>> class foo:

    .... def __init__(self, a):
    .... self.a = a
    .... def __str__(self):
    .... return 'foo(%s)' % self.a
    ....
    >>> set([foo(1)])

    set([<__main__.foo instance at 0x009D8030>])
    >>> class foo:

    .... def __init__(self, a):
    .... self.a = a
    .... def __repr__(self):
    .... return 'foo(%s)' % self.a
    ....
    >>> set([foo(1)])

    set([foo(1)])

    Were you just looking for this behavior, or were you asking why it's __repr__
    and not __str__?

    Steve
     
    Steven Bethard, Oct 22, 2004
    #2
    1. Advertising

  3. Chris S.

    Chris S. Guest

    Steven Bethard wrote:

    > Were you just looking for this behavior, or were you asking why it's __repr__
    > and not __str__?
    >
    > Steve


    Actually, it seems I'm confusing __str__ with __repr__. Now things make
    sense. Thanks for the clarification.
     
    Chris S., Oct 22, 2004
    #3
  4. Chris S. <chrisks <at> NOSPAM.udel.edu> writes:
    >
    > Actually, it seems I'm confusing __str__ with __repr__. Now things make
    > sense. Thanks for the clarification.


    You're welcome. Glad it was the easy question, not the hard one -- I never
    did bother to look up the __repr__ vs. __str__ discussions on this topic. ;)

    Steve
     
    Steven Bethard, Oct 22, 2004
    #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. David
    Replies:
    2
    Views:
    503
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    662
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    356
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,839
    Oliver Wong
    Aug 3, 2006
  5. maestro
    Replies:
    1
    Views:
    324
    Chris
    Aug 11, 2008
Loading...

Share This Page