I would like to know if there is a way to create a list
that will execute some actions each time I use the method append
(or an other similar method).
I know that I could create a class
that inherits from list
and overwrite append
, remove
and all other methods that change content of list
but I would like to know if there is an other way.
By comparison, if I want to print
'edited' each time I edit an attribute of an object I will not execute print("edited")
in all methods of the class
of that object. Instead, I will only overwrite __setattribute__
.
I tried to create my own type which inherits of list
and overwrite __setattribute__
but that doesn't work. When I use myList.append
__setattribute__
isn't called. I would like to know what's realy occured when I use myList.append
? Is there some magic methods called that I could overwrite ?
I know that the question have already been asked there : What happens when you call `append` on a list?. The answer given is just, there is no answer... I hope it's a mistake.
I don't know if there is an answer to my request so I will also explain you why I'm confronted to that problem. Maybe I can search in an other direction to do what I want. I have got a class
with several attributes. When an attribute is edited, I want to execute some actions. Like I explain before, to do this I am use to overwrite __setattribute__
. That works fine for most of attributes. The problem is lists
. If the attribute is used like this : myClass.myListAttr.append(something)
, __setattribute__
isn't called while the value of the attribute have changed.
The problem would be the same with dictionaries
. Methods like pop
doesn't call __setattribute__
.
Aucun commentaire:
Enregistrer un commentaire