Wednesday, 25 January 2017

CONSTRUCTOR IN PYTHON

It is a special kind of method which executes automatically whenever we create object (i.e. without calling it). Contructors are used to initialize the non-static variables of the class at the time of creation of object with the user defined value.

Example –
class A:
    def __init__(self):
        print 'in const of A'
A1=A()
 
Output –
in const of A
 
Example –
class A:
    def __init__(self,i,j):
        self.i=i
        self.j=j
A1=A(10,12.12)
print A1.i
print A1.j
A2=A(500,21.25)
print A2.i
print A2.j
 
Output –
10
12.12
500
21.25

No comments:

Post a Comment