Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Recursive Str?

Reply
Thread Tools

Recursive Str?

 
 
Chris S.
Guest
Posts: n/a
 
      10-22-2004
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))
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      10-22-2004
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

 
Reply With Quote
 
 
 
 
Chris S.
Guest
Posts: n/a
 
      10-22-2004
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.
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      10-22-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive functions Vs Non-recursive functions - performance aspect vamsi C Programming 21 03-09-2009 10:53 PM
Two recursive calls inside of a recursive function n00m C++ 12 03-13-2008 03:18 PM
Recursive function Tyron VHDL 1 04-16-2004 07:20 AM
recursive description with generate and processes Joerg Ritter VHDL 10 12-06-2003 10:24 PM
NCVHDL/NCELAB and Recursive Instantiation Anand P Paralkar VHDL 2 11-10-2003 06:14 PM



Advertisments