overloading print behavior

Cere Davis cere at u.washington.edu
Mon Apr 28 06:10:30 CEST 2003


So I have a function like:

class C:
    def __init__(self):
       self.a="asdf"
       self.b="123"
c=C()
st=["abc",c]

When I:

print st

I get:

['abc', <__main__.C instance at 0x40288fac>]

Which is what I expect,  but I would like it if print or perhaps the 
str() coercion that is happening behind the scenes would walk through my 
C instance
and resolve the name/value paris within the C class and print the 
values.  Something like:

print st

 > 'abc',<C instance>['a=asdf','b=123']

I'm not sure what the best way to present this would be but I'm lead to 
believe that there is a clever way to do this by registering the 
name/value info of
the variables in the C class with __setitem__ /__getitme__ but I am not 
interly sure how this works...or if it works.  Could someone enlighten 
me on this simple concept?

Thanks,
Cere






More information about the Python-list mailing list