-Until recently I believed that classes were 'molds' that were used to create objects; however, this question ( Why can't you delete methods in python? ) made me question a few things. What is said in the most voted answer is that, in short, you can delete methods since, just like anything else, methods (without parentheses) are basically pointers that store the reference ('point') to the method code that loads into RAM. In other words: they are attributes of all life except that, in this case, they point to code and not to normal data, therefore when using the reserved worddel
what we do is eliminate the references that pointer saves, which translates into 'cutting' the 'link' that pointer has with the variable(s) to which it points (from what I also understand can save multiple references, it would be the same as what would happen with lists for example, at least that's how it works in C
and I guess it will be the same as what happens in python (if not, please comment) therefore we can no longer access nothing through it, since it no longer points anywhere...
- So far so good, the question begins when it is mentioned that objects inherit from the classes from which they are instantiated, it is for the same reason that a method cannot be eliminated through an object, since that method is not yours but of your class , this makes a lot of sense, since (as you can see in the post) starting from that premise everything works.
-With which, the question would be the following: Are the classes really ' molds '? What actually happens when we create and instantiate an object from a class? (removing all the abstraction layers, which is what actually happens in RAM)
PLOT
-I understood the following...
class persona:
def __init__(self, nombre, apellido, edad, sexo) -> None:
self.nombre = nombre
self.apellido = apellido
self.edad = edad
self.sexo = sexo
persona_nueva = persona('Jhon','Doe', 33, 'h')
-In this case, it persona_nueva
would then be a pointer that would store the reference to an object (in this case we can understand an object as a set of pointers), therefore, execute ...
print(persona_nueva.nombre)
-It would be like saying to persona_nueva
(apart from the participation of _ _ str _ _): 'pass print
the value of the place where the pointer you are pointing to points to, that nombre
' (this would be more or less the structure that is shape in my head)
____-nombre -> 'Jhon'
/ -apellido -> 'Doe'
persona_nueva -edad -> 33
-sexo -> 'h'
already persona_nueva
agreed to nombre
...
persona_nueva.nombre -> print
It's for the same reason that it del persona_nueva.nombre
works (at least on my machine), as it would be equivalent to...
____-nombre -> 'Jhon'
\ -apellido -> 'Doe'
persona_nueva -edad -> 33
-sexo -> 'h'
and therefore ...
-apellido -> 'Doe'
persona_nueva -edad -> 33
-sexo -> 'h'
-Well, the case is that if it were a method (that is, if the name, for example, were a method) and therefore...
-nombre -> nombre()
-apellido -> 'Doe'
persona_nueva -edad -> 33
-sexo -> 'h'
In this case, it could not be executed anymore del persona_nueva.nombre
... This is basically what I asked in (Why can't you delete methods in python?), in response I was told that it is because technically, the attributes are not of persona_nueva
(object ) but of its class persona
, but if this is the case, why can I remove attributes (use del
) from persona_nueva
it and not methods, these having the same behavior. Most likely it's because I don't know what a class is, hence the question...