I am puzzled by what, in my opinion, is a very simple and direct subclass of a list in Python.
Suppose I need all the functionality of a list. I want to add several methods to the default list set.
The following is an example:
class Mylist(list): def cm1(self): self[0]=10 def cm2(self): for i,v in enumerate(self): self[i]=self[i]+10 def cm3(self): self=[] def cm4(self): self=self[::-1] def cm5(self): self=[1,2,3,4,5] ml=Mylist([1,2,3,4]) ml.append(5) print "ml, an instance of Mylist: ",ml ml.cm1() print "cm1() works: ",ml ml.cm2() print "cm2() works: ",ml ml.cm3() print "cm3() does NOT work as expected: ",ml ml.cm4() print "cm4() does NOT work as expected: ",ml ml.cm5() print "cm5() does NOT work as expected: ",ml
Exit:
Mylist: [1, 2, 3, 4, 5] cm1() works: [10, 2, 3, 4, 5] cm2() works: [20, 12, 13, 14, 15] cm3() does NOT work as expected: [20, 12, 13, 14, 15] cm4() does NOT work as expected: [20, 12, 13, 14, 15] cm5() does NOT work as expected: [20, 12, 13, 14, 15]
So, it seems that scalar assignment works as I expect and understand. List or snippets do not work, as I understand it. "Doesn't work", I mean that the code in the method does not change the ml instance, as the first two methods do.
What do I need to do to work cm3() cm4() and cm5() ?