473,414 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Recursive Str?

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))
Jul 18 '05 #1
3 2047
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

Jul 18 '05 #2
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.
Jul 18 '05 #3
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
0
by: champ1979 | last post by:
I wrote an algorithm to get all the relatives of a person in a family tree. I'm basically getting all the users from the DB and am doing the recursive logic in code, so that there is only 1 call...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.