-The question would be the following: why can't the methods be eliminated through the use of the keyworddel
-In the case of decorating them with a deleter, it would not be the same, since in such cases the objects to which the method decorated with is relative would be eliminated @property
, not the method itself
-Let's remember that after all, functions are (just like everything in python) objects, pointers that point to variables, in this case, said variables would be the code block that loads into RAM, for example...
def saludo():
print('Hola como estamos por aca? ')
-In this case saludo
itself, without the use of the dunder __call__
(that is, without calling the parentheses), it is a pointer that points to saludo()
, it is for the same reason as with these if we can use the keyword del
without errors, since (if I'm not wrong, it's what my intuition tells me, if you have reasons to say that I'm wrong about something, please say so), what it does del
is basically make saludo
(as a pointer) point to None
. And therein lies the question, because if functions and methods are basically the same, they cannot be removed as easily.
-Note that deleting a method given that it is decorated with a deletter
, does not enter into my perception of deletion, since, as I mentioned at the beginning, in such cases the attributes to which that method is relative would be deleted, not the method itself.
Of course you can remove methods in python. A method is nothing more than an attribute and as you indicate in the question it is a kind of "pointer" to the function code.
A function is at heart an object (of a special "callable" type) and the function name is nothing more than a symbol pointing to the function. When you delete that symbol with
del simbolo
, you simply "break" the connection between that symbol and the object it pointed to. The object is left undefined (which is not the same as with valueNone
), and an attempt to access that symbol will fail:The symbol
f
has ceased to exist, but the object (function) it pointed to may not. In particular, if you have another symbol pointing to the same object, the object will still exist:As you can see,
del f
it does not remove the function itself, but only the symbolf
that pointed to the function. The function still exists and can be called via the symbolg
. Only when all the symbols that pointed to that object are gone is the object finally removed from memory by the garbage collector.And the methods?
The same thing happens with methods, only there is a nuance. A method is nothing more than an attribute that "points" to a function, but that attribute can exist as part of the object or as part of the class. Let's first see an example with attributes that are not methods, but for example integers:
The class
A
has an attribute calledx
. The objecta
(which is an instance of the classA
) has another attribute calledy
. Although the user may try to accessa.x
, it does not actuallyx
exist as part ofa
, but as part of the class (and is "inherited" by the object).This implies that we can delete
a.y
but nota.x
becausea
it does not actually have the attribute x:Instead there is no problem in removing it from the class (which is where it actually exists):
Naturally once we have deleted it from the class, any subsequent instance will not be able to inherit it:
Exactly the same thing happens with methods . Except that in almost all cases the methods belong to the class, and not to the object. Therefore if you want to delete them you must do it through the class: