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) 4 1835
"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
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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michele Simionato |
last post by:
I have read with interest the recent thread about closures. The funny
thing is that the authors are arguing one against the other but I
actually agree with all of them and I have a proposal that...
|
by: Jonas Koelker |
last post by:
Hello there.
I'm new to this list (/me welcomes himself).
anyways, I'm having doubts about how to subclass
properly. The big picture is that I want to handle
permutations. As we all know, two...
|
by: BJörn Lindqvist |
last post by:
A problem I have occured recently is that I want to subclass builtin
types. Especially subclassing list is very troublesome to me. But I
can't find the right syntax to use. Take for example this...
|
by: Andrew Jaffe |
last post by:
Hi,
I have a class with various class-level variables which are used to
store global state information for all instances of a class. These are
set by a classmethod as in the following (in...
|
by: Pieter Linden |
last post by:
Hi,
This question refers sort of to Rebecca Riordan's article on Access
web about subclassing entities:
http://www.mvps.org/access/tables/tbl0013.htm
How practical is this? I am writing a...
| |
by: TheStripe |
last post by:
Hello all,
Wondering if anyone has come across this problem I am having..
I am Subclassing a RadioButtonList to add some properties that I need. I am
building up
a table and adding to a...
|
by: Kevin Prichard |
last post by:
Hi all,
I've recently been following the object-oriented techiques discussed
here and have been testing them for use in a web application. There is
problem that I'd like to discuss with you...
|
by: manatlan |
last post by:
I've got an instance of a class, ex :
b=gtk.Button()
I'd like to add methods and attributes to my instance "b".
I know it's possible by hacking "b" with setattr() methods. But i'd
like to do...
|
by: xamalek |
last post by:
I was thinking that when you override a struct variable when you are
subclassing the struct, then the variable in the new struct would
stomp on the old one ... (i.e.) the parent variable (i) and...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |