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

subclassing list and adding other variables ?

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

and below are some real unsuccessful tests :
class Test(tuple): def __init__(self, a, alist):
self.a = a
self = tuple(alist)

t = Test(1, (1,2,3))
Traceback (most recent call last):
File "<pyshell#486>", line 1, in -toplevel-
t = Test(1, (1,2,3))
TypeError: tuple() takes at most 1 argument (2 given) class Test(tuple): def __init__(self, (a, alist)):
self.a = a
self = tuple(alist)

t = Test((1, (1,2,3)))
t.a 1 t.a = 2
t.a 2 len(t) 2 t (1, (1, 2, 3)) t[2]
Traceback (most recent call last):
File "<pyshell#495>", line 1, in -toplevel-
t[2]
IndexError: tuple index out of range t[1] (1, 2, 3) class Test(tuple): def __init__(self, (a, alist)):
self.__init__(tuple(alist))
self.a = a

t = Test((1, (1,2,3)))
Traceback (most recent call last):
File "<pyshell#500>", line 1, in -toplevel-
t = Test((1, (1,2,3)))
File "<pyshell#499>", line 3, in __init__
self.__init__(tuple(alist))
File "<pyshell#499>", line 2, in __init__
def __init__(self, (a, alist)):
ValueError: unpack tuple of wrong size class Test(tuple): def __init__(self, alist, a = None):
self.__init__(tuple(alist))
self.a = a

t = Test((1,2,3), 4)
Traceback (most recent call last):
File "<pyshell#503>", line 1, in -toplevel-
t = Test((1,2,3), 4)
TypeError: tuple() takes at most 1 argument (2 given) class Test(tuple): def __init__(self, (alist, a = None)):
self.__init__(tuple(alist))
self.a = a

SyntaxError: invalid syntax
class Test(list): def __init__(self, (a, alist)):
self.a = a
self = tuple(alist)

t = Test((4, (1,2,3)))
t [] class Test(list): def __init__(self, (a, alist)):
self.a = a
self = alist

t = Test((4, (1,2,3)))
t [] t.a 4 len(t) 0 class Test(list): def __init__(self, a, alist):
self.a = a
self = alist

t = Test(4, (1,2,3))
t [] t.a 4 class Test(list): def __init__(self, a, alist):
self.a = a
self.__init__(alist)

t = Test(4, (1,2,3))


Traceback (most recent call last):
File "<pyshell#523>", line 1, in -toplevel-
t = Test(4, (1,2,3))
File "<pyshell#522>", line 4, in __init__
self.__init__(alist)
TypeError: __init__() takes exactly 3 arguments (2 given)
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"GrelEns" <gr*****@NOSPAMyahoo.NOTNEEDEDfr> a écrit dans le message de news:
40*********************@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
Jul 18 '05 #2

P: n/a
GrelEns wrote:
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 ?


For subclassing builtin types, read:

http://www.python.org/2.2.3/descrintro.html#subclassing

Gerrit.

--
Weather in Twenthe, Netherlands 11/03 20:25 UTC:
2.0°C wind 2.7 m/s E (57 m above NAP)
--
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #3

P: n/a
GrelEns wrote:
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 ?


[You tried hard]

With lists it is the standard procedure of overriding __init__() and calling
the baseclass method:
class List(list): .... def __init__(self, iterable, a):
.... list.__init__(self, iterable)
.... self.a = a
.... a = List((1,2,3), "abc")
a [1, 2, 3] a.a 'abc'

Tuples are immutable and thus changes in __init__() will not affect the
tuple items. They are set in the __new__() method instead.
class Tuple(tuple): .... def __new__(cls, *args):
.... return tuple.__new__(cls, args[0])
.... def __init__(self, seq, a):
.... self.a = a
.... b = Tuple((1,2,3), "abc")
b (1, 2, 3) b.a 'abc'

For a minimal iterable class, let __iter__() return self and next()
calculate the next value or raise a StopIteration exception when there are
no more values:
class Iterable: .... def __init__(self, start, maxval):
.... self.value = start
.... self.maxval = maxval
.... def __iter__(self): return self
.... def next(self):
.... if self.value > self.maxval:
.... raise StopIteration
.... result = self.value
.... self.value *= -2
.... return result
.... for n in Iterable(1, 500):

.... print n,
....
1 -2 4 -8 16 -32 64 -128 256 -512

(I've got a hunch that Iterator would have been a more appropriate name, but
you may judge on your own, see
http://www.python.org/doc/current/tut/node17.html)

Peter
Jul 18 '05 #4

P: n/a

"Peter Otten" <__*******@web.de> a écrit dans le message de news:
c2*************@news.t-online.com...
GrelEns wrote:
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 ?
With lists it is the standard procedure of overriding __init__() and

calling the baseclass method:
class List(list): ... def __init__(self, iterable, a):
... list.__init__(self, iterable)
... self.a = a
... a = List((1,2,3), "abc")
a [1, 2, 3] a.a 'abc'


thanks, so i supposed that this is a correct way to do :
class List(list): def __init__(self, v = None):
self.v = v
def load(self, values):
list.extend(self, values)
def gets(self):
return list(self)
l = List(5)
l.load([1,2,3])
l [1, 2, 3] l.gets() [1, 2, 3] type(l.gets()) <type 'list'> type(l)

<class '__main__.List'>

BTW while i think i get it with the list.extend(self, values) which is i
suppose

super_class.method_from_super_class(my_current_ins tance, *ohers_args)

i feel quite unhappy with the return list(self) which for me is rather a
call to the builtin function list() upon the current instance , how could i
write this particular :

super_class.attribute_containing_content_of(my_cur rent_instance)

also, which are the method to overwrite for a class subclassing a list to
automatically call the load() method if the list is empty ?

thx for your help.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.