subclassing list and adding other variables ?

GrelEns grelens at NOSPAMyahoo.NOTNEEDEDfr
Thu Mar 11 15:57:12 EST 2004


"GrelEns" <grelens at NOSPAMyahoo.NOTNEEDEDfr> a écrit dans le message de news:
4050d0df$0$289$626a14ce at news.free.fr...
> hello,
>
> i wonder if this possible to subclass a list or a tuple and add more
> attributes ? also does someone have a link to how well define is own
> iterable object ?
>
> what i was expecting was something like :
>
> >>> t = Test('anAttributeValue', ['el1', 'el2'])
> >>> t.anAttribute
> 'anAttributeValue'
> >>> for x in t:
>        print x
> el1
> el2

i reply to myself, but still have questions ;)

so this one works :

>>> class Test(list):
 def __init__(self, a):
  self.a = a
 def load(self, alist):
  self.extend(alist)


>>> t = Test(4)
>>> t
[]
>>> t.load((1,2,3))
>>> t
[1, 2, 3]

what do you think of such a design ? is there some underlying flaw that i
should be aware of ?

(and about the tuple stuffs not working in my previous examples, i suppose
this is because a tuple is immutable and thus should not be modified after
creation)

thx





More information about the Python-list mailing list