Making a list-inhereted object return standard repr?

Delaney, Timothy tdelaney at avaya.com
Sun Feb 3 18:01:15 EST 2002


> From: Philip Swartzleonard [mailto:starx at pacbell.net]
> 
> Jason Orendorff || Fri 01 Feb 2002 08:32:57p:
> 
> > Philip Swartzleonard wrote:
> >> Ok, so i decided to make my container class inherit from 
> list, [...]
> >> I want to change __repr__ and __str__ to do something more like the
> >> first case again [...] 
> > 
> > class Foo(list):
> >     def __repr__(self):
> >         basic = object.__repr__(self)
> >         values = list.__repr__(self)
> >         return basic[:-1] + ' containing ' + values + '>'
> > 
> > ## Jason Orendorff    http://www.jorendorff.com/
> 
> Thanks! That's copy-and-paste'ly what i need. I knew it was 
> something more 
> or less obvious i was missing %D.
> 
> (although i may have to leave out the contains, as it gives 
> me things like     
> <__main__.Cell object at 0x015F0210 containing 
> [<__main__.Player object at  
> 0x015E7870 containing []>]> ... so far every object is a container :)

This brings up the interesting question of whether there should be a an
object method which returns the "canonical repr" of an object i.e. the
<module.Class object at address> or possibly even a builtin method (so it
would work with classic classes too).

I know I've wanted to use this in classic classes many times for debugging
purposes - I want to be easily able to distinguish instances from each
other. Each time I end up having to code it myself.

Tim Delaney




More information about the Python-list mailing list