Constructor
A constructor is a special method of class. It is special because it has the same name as its class and by default available in the class definition. The constructor invokes the ‘__init__’ method automatically each time you create an object. The __init__() method is a method by default available to any class definition that is used to initialise the instance variables of class.
Class Variables and Instance Variables
There are two types of variables we can see inside a class definition. Class variables and instance variables. Class variables are those variables that are initialised inside class definition. Those will have a fixed value for all the instances of the class
#class variable example
class human:
eyes=2
m1=human()
m2=human()
print(m1.eyes)
print(m2.eyes)
An instance variable is the one that will have different values for each instance declared out of that class. The instance variable usually get initialised by the constructor.
#instance variable example
class human:
eyes=2
def __init__(self,name,skincolor):
self.name=name
self.skincolor=skincolor
m1=human(“ram”,”blue”)
m2=human(“sita”,”fair”)
print(m1.eyes,” “,m1.name,” “,m1.skincolor)
print(m2.eyes,” “,m2.name,” “,m2.skincolor)