The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.
The purpose of the __init__() method is to initialize the class. It is usually responsible for populating the instance variables. Because of this, you want to have __init__() get called for all classes in the class hierarchy.
In this code:
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
... the 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
The __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.
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
The __init__
method gets called after 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
Your x
and 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._x
and self._y
sets those variables as members of the Point
object (accessible for the lifetime of the object).
N.B. Some clarification of the use of the word "constructor" in this answer. Technically the responsibilities of a "constructor" are split over two methods in Python. Those methods are __new__
(responsible for allocating memory) and __init__
(as discussed here, responsible for initialising the newly created instance).
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 __init__
function:
class MyClass(object):
i = 123
def __init__(self):
self.i = 345
a = MyClass()
print(a.i)
print(MyClass.i)
Output:
345
123
In short:
self
as it suggests, refers to itself- the object which has called the method. That is, if you have N objects calling the method, then self.a
will refer to a separate instance of the variable for each of the N objects. Imagine N copies of the variable a
for 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 createdAttribute 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'
then MyClass.i
and 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.
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)
You get:
> Buy 5 apples 6 figs and 7 dates. Total fruitty bill is 18 pieces of
> fruit :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With