What __init__ and self do on Python?
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__(?): .... ....
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.
In this code:
class A(object): def __init__(self): self.x = 'Hello' def method_a(self, foo): print self.x + ' ' + foo
self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the
A class and call its methods, it will be passed automatically, as in ...
a = A() # We do not pass any argument to the __init__ method a.method_a('Sailor!') # We only pass a single argument
__init__ method is roughly what represents a constructor in Python. When you call
A() Python creates an object for you, and passes it as the first parameter to the
__init__ method. Any additional parameters (e.g.,
A(24, 'Hello')) will also get passed as arguments--in this case causing an exception to be raised, since the constructor isn't expecting them.
Read more... Read less...
Yep, you are right, these are oop constructs.
__init__ is the constructor for a class. The
self parameter refers to the instance of the object (like
this in C++).
class Point: def __init__(self, x, y): self._x = x self._y = y
__init__ method gets called when memory for the object is allocated:
x = Point(1,2)
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:
class Point: def __init__(self, x, y): _x = x _y = y
y parameters would be stored in variables on the stack and would be discarded when the init method goes out of scope. Setting those variables as
self._y sets those variables as members of the
Point object (accessible for the lifetime of the object).
A brief illustrative example
In the hope it might help a little, here's a simple example I used to understand the difference between a variable declared inside a class, and a variable declared inside an
class MyClass(object): i = 123 def __init__(self): self.i = 345 a = MyClass() print(a.i) print(MyClass.i)
selfas it suggests, refers to itself- the object which has called the method. That is, if you have N objects calling the method, then
self.awill refer to a separate instance of the variable for each of the N objects. Imagine N copies of the variable
afor each object
__init__is what is called as a constructor in other OOP languages such as C++/Java. The basic idea is that it is a special method which is automatically called when an object of that Class is created
__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.
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:
class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world'
MyClass.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 of
MyClass.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:
x = MyClass()
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:
def __init__(self): self.data = 
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:
x = MyClass()
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,
class Complex: def __init__(self, realpart, imagpart): self.r = realpart self.i = imagpart x = Complex(3.0, -4.5) x.r, x.i
Taken from official documentation which helped me the most in the end.
Here is my example
class Bill(): def __init__(self,apples,figs,dates): self.apples = apples self.figs = figs self.dates = dates self.bill = apples + figs + dates print ("Buy",self.apples,"apples", self.figs,"figs and",self.dates,"dates. Total fruitty bill is",self.bill," pieces of fruit :)")
When you create instance of class Bill:
purchase = Bill(5,6,7)
> Buy 5 apples 6 figs and 7 dates. Total fruitty bill is 18 pieces of > fruit :)