Re: list extension ?
- From: Bruno Desthuilliers <bruno.42.desthuilliers@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Jun 2008 12:08:58 +0200
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.
.
- Follow-Ups:
- Re: list extension ?
- From: Stef Mientki
- Re: list extension ?
- From: Stef Mientki
- Re: list extension ?
- References:
- list extension ?
- From: Stef Mientki
- list extension ?
- Prev by Date: Re: what is meaning of "@" in pyhon program.
- Next by Date: Re: Getting sorting order
- Previous by thread: Re: list extension ?
- Next by thread: Re: list extension ?
- Index(es):