By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,661 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,661 IT Pros & Developers. It's quick & easy.

list extension ?

P: n/a
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 ) :
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

a = [ 2, 3 ]
a.New_Attribute = 'some text' <== ERROR

Can someone explain this different behavior ?

thanks,
Stef Mientki
Jun 29 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
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
Sc***********@Acm.Org
Jun 29 '08 #2

P: n/a
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.
Jun 30 '08 #3

P: n/a
thanks guys,
>
> 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)
that's even better, except the call must be
list.__init__ ( self, args )

cheers,
Stef
Jun 30 '08 #4

P: n/a
thanks guys,
>
> 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)
that's even better, except the call must be
list.__init__ ( self, args )

cheers,
Stef
Jun 30 '08 #5

P: n/a
On 30 juin, 21:05, Stef Mientki <stef.mien...@gmail.comwrote:
thanks guys,
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:
Ah hem.... Sorry, should have double-checked:
def __init__(self, *args)
list.__init__ ( self, value )
list.__init__(self, *args)

that's even better, except the call must be
list.__init__ ( self, args )
Indeed. Which makes my whole comment about "following parent's class
behaviour" totally off-tracks. Should have shut up here :(
Jun 30 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.