I'm learning the Python programming language and I've came across something I don't fully understand.
In a method like:
def method(self, blah):
def __init__(?):
....
....
What does self
do? What is it meant to be? Is it mandatory?
What does the __init__
method do? Why is it necessary? (etc.)
I think they might be OOP constructs, but I don't know very much.
__init__
does act like a constructor. You'll need to pass "self" to any class functions as the first argument if you want them to behave as non-static methods. "self" are instance variables for your class.Yep, you are right, these are oop constructs.
__init__
is the constructor for a class. Theself
parameter refers to the instance of the object (likethis
in C++).The
__init__
method gets called when memory for the object is allocated:It is important to use the
self
parameter inside an object's method if you want to persist the value with the object. If, for instance, you implement the__init__
method like this:Your
x
andy
parameters would be stored in variables on the stack and would be discarded when the init method goes out of scope. Setting those variables asself._x
andself._y
sets those variables as members of thePoint
object (accessible for the lifetime of the object).Try out this code. Hope it helps many C programmers like me to Learn Py.
Output:
Jay
Sum = 5
Doc - Inside Class
Doc - __init__ Constructor
Doc - Inside Show
Destructor Deleting object - Jay
Basically, you need to use the 'self' keyword when using a variable in multiple functions within the same class. As for init, it's used to setup default values incase no other functions from within that class are called.
Class objects support two kinds of operations: attribute references and instantiation
Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the class definition looked like this:
then
MyClass.i
andMyClass.f
are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value ofMyClass.i
by assignment.__doc__
is also a valid attribute, returning the docstring belonging to the class: "A simple example class".Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example:
The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named
__init__()
, like this:When a class defines an
__init__()
method, class instantiation automatically invokes__init__()
for the newly-created class instance. So in this example, a new, initialized instance can be obtained by:Of course, the
__init__()
method may have arguments for greater flexibility. In that case, arguments given to the class instantiation operator are passed on to__init__()
. For example,Taken from official documentation which helped me the most in the end.
Here is my example
When you create instance of class Bill:
You get: