1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
class MyList (list):
def __init__ (self, args):
self.ml = list (args)
def __add__(self, args):
#print ('overload add')
if type (args) == list ():
return MyList (self.ml [:] + args)
else:
return MyList (self.ml [:] + list (args))
def __getattr__(self, name):
#print ('attribute fetch')
return MyList (getattr(self.ml, name))
def __getitem__(self, offset):
#print ('attribute fetch item')
return self.ml [offset]
def __repr__(self):
return repr (self.ml)
class MyListSub (MyList):
numCall = 0
def __init__ (self, args):
MyListSub.numCall += 1
self.sCall = 0
print ('MyListSub, num call: %s' % self.numCall)
return MyList.__init__ (self, args)
def __add__ (self, args):
self.sCall += 1
print ('add of MyListSub, num call: %s' % self.sCall)
return MyList.__add__(self, args)
def stats (self):
return self.numCall, self.sCall
if __name__ == '__main__':
a = MyListSub ('test')
a = a + 'other'
b = MyListSub ('spam')
b = b + 'add'
b = b + 'bacon'
print (a, b)
print (a.stats ())
print (b.stats ()) |
Partager