Recursive Str?

Chris S. chrisks at NOSPAM.udel.edu
Fri Oct 22 02:49:57 EDT 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))



More information about the Python-list mailing list