I know how to use magical methods in python, but I would like to understand more about them.
For it I would like to consider three examples:
1) __init__:
We use this as constructor in the beginning of most classes. If this is a method, what is the object associated with it? Is it a basic python object that is used to generate all the other objects?
2) __add__ We use this to change the behaviour of the operator +. The same question above.
3) __name__: The most common use of it is inside this kind of structure:if __name__ == "__main__":
This is return True when you are running the module as the main program.
My question is __name__ a method or a variable? If it is a variable what is the method associated with it. If this is a method, what is the object associated with it?
Since I do not understand very well these methods, maybe the questions are not well formulated. I would like to understand how these methods are constructed in Python.
__init__gets called, if present, around object construction time. A module’s__name__is set by the system, etc…