list extension ?

Scott David Daniels Scott.Daniels at Acm.Org
Sun Jun 29 23:40:44 CEST 2008


Stef Mientki wrote:
... (approximately, I PEP-8'ed it a bit) ...
> class tGrid_List(list):
>    def __init__(self, value=[]):
>        list.__init__(self, value)
> # and with this new list component, I can add new attributes on the fly
> a = tGrid_list([2, 3])
> a.New_Attribute = 'some text'
> 
> # I'm not allowed to this with the standard list
> Can someone explain this different behavior ?

Yes, you did not add a __slots__ member, so the object will have a
__dict__ attribute.  The __slots__ stuff is a storage optimization
for frequently used data types (not worth it if you don't plan to
have more than several thousand at any one time).  Lists (for that
matter most Python builtin types) get used a lot, so they have that
built in.

You can observe that even on the most trivial extensions:

     class AttributableInt(int): pass
     class AttributeFreeInt(int): __slots__ = ()

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list