list extension ?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Jun 30 06:08:58 EDT 2008


Stef Mientki a écrit :
> hello,
> 
> I basically need a list with a few extra attributes,
> so I derived a new object from a list, and it works perfect.
> But I wonder why the newly derived list component is much more flexible ?
> 
> # so here is the new list object
> class tGrid_List ( list ) :

pep08: class GridList(list):

>    def __init__ ( self, value = [] ) :

Gotcha : default argument values are eval'd only once. Also, it would 
make more sense IMHO to follow the parent's class initializer's behaviour:

     def __init__(self, *args)

>        list.__init__ ( self, value )

          list.__init__(self, *args)

> # and with this new list component, I can add new attributes on the fly
> 
> a = tGrid_list ( [ 2, 3 ] )

a = GridList(2, 3)

or
l = [2, 3]
a = GridList(*l)

> a.New_Attribute = 'some text'
> 
> # I'm not allowed to this with the standard list
> 
> a = [ 2, 3 ]
> a.New_Attribute = 'some text'     <== ERROR
> 
> Can someone explain this different behavior ?

Most builtin types are optimized, and not having a __dict__ is part of 
this optimization.



More information about the Python-list mailing list