Python: usage of self and __init__

I was reading this book dive into python and thought this would be helpful. So..
When defining your class methods, you must explicitly list self as the first argument for each method, including __init__. When you call a method of an ancestor class from within your class, you must include the self argument. But when you call your class method from outside, you do not specify anything for the self argument; you skip it entirely, and Python automatically adds the instance reference for you.

__init__ methods are optional, but when you define one, you must remember to explicitly call the ancestors __init__ method (if it defines one). This is more generally true: whenever a descendant wants to extend the behavior of the ancestor, the descendant method must explicitly call the ancestor method at the proper time, with the proper arguments.

Leave a Reply

Your email address will not be published. Required fields are marked *