I have a question about methods in python. According to the Python documentation on the special attribute __dict__
:
A dictionary or other mapping object used to store an object's (writable) attributes .
Translation:
A dictionary or other mapping object used to store the (write) attributes of an object.
In a nutshell the attribute __dict__
stores the attributes of an object.
When creating, for example, the following class:
>>> class A:
... a = 1
...
... def method(self):
... print('method')
...
...
>>> A.__dict__
{'__module__': '__main__', 'a': 1, 'method': <function A.method at 0x7f4b2e802950>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
You can see that the "method" method
is in __dict__
the class, as well as the attribute a
.
According to the python documentation, __dict__
it stores the attributes of an object (it should be noted that a class is an object). So a method is actually an attribute?
Could it be said that it method
is an attribute that stores a function? But at no time did I do something like this:
def funcion(self): print(self.a) class A: a = 1 method = funcion print(A().method())
In addition to that it works and the function
function
receives as first parameter (self
), the instance, printing1
as result.Here another question arises, the attribute
method
that I define in the classA
(method = funcion
), is it a method?
So what exactly is a method in python?
It would be of great help if you could clarify this doubt that I have, thanks for your answers.