How to make a class method in place?

Viewed 712

Is there a way to make a method in a python class modify its data in place, specifically for lists?

For example, I want to write a function that behave like list.append() by modifying the origonal list instead of returning a new one

I have already

class newlist(list):
     def add(self, otherlist):
          self = self+otherlist

A method written like that does not seem to modify the variable it is called on. Obviosly, I could add return self at the end, but then it would have to be called with mylist = mylist.add(stuff) to actually modify mylist. How do write the function so it will modify mylist when called with just mylist.add(stuff)?

3 Answers
Related