as the obskyr answer suggests, you must define a child class listand override many methods and thoroughly test to see if you notice something.
, __getattribute__ ( ), __iadd__ ( +=) __setitem__ ( ), , :
class MyList(list):
def __getattribute__(self,a):
if a in {"append","extend","remove","insert","pop","reverse","sort","clear"}:
print("modification by {}".format(a))
else:
print("not modified {}".format(a))
return list.__getattribute__(self,a)
def __iadd__(self,v):
print("in place add")
return list.__iadd__(self,v)
def __setitem__(self,i,v):
print("setitem {},{}".format(i,v))
return list.__setitem__(self,i,v)
l = MyList()
l.append(12)
l.extend([12])
l.remove(12)
print(l)
l[:] = [4,5,6]
l += [5]
print(l)
:
modification by append
modification by extend
modification by remove
[12]
setitem slice(None, None, None),[4, 5, 6]
in place add
[4, 5, 6, 5]
, , .