Constructor:
class emp:
##constructor
def __init__(self,name,id,sal):
self.id=id
self.name=name
self.sal=sal
## defining the method
def display(self):
print("empid:%d\n
ename:%s\nsalary:%f"%(self.id,self.name,self.sal))
e1=emp("ravi",10,1500)
e2=emp("sita",20,2500)
e1.display()
e2.display()
"""output:
empid:10
ename:ravi
salary:1500.000000
empid:20
ename:sita
salary:2500.000000"""
It is a special method, it is called
automatically as soon as the object is created. Constructor are used to initialise
the data into the object.
No need to call the
constructors, once object the object is created, it is called automatically.
There are two types of
constructors:
1.
Parameterised constructor
2.
Non-parameterised
constructor
Parameterised constructor: if atleast one
parameter is there is called parameterised constructor.
Ex:
def __init__(self,id,name,sal):
….
Ex: E1=emp(10,’sita’,1500)
Non-parameterised
constructor:
If there is no parameter in
the constructor then it is called non-parameterised constructor. Except ‘self’
keyword.
Ex:
def __init__(self):
….
e.display()
Related Video: https://youtu.be/cm7EW6q-tR0
Next:
Previous: Example program on class and object
Easy "water hack" burns 2 lbs OVERNIGHT
ReplyDeleteAt least 160,000 men and women are utilizing a easy and SECRET "water hack" to lose 1-2 lbs every night while they sleep.
It's easy and it works with everybody.
Here's how to do it yourself:
1) Grab a glass and fill it up half glass
2) And now do this strange HACK
and you'll become 1-2 lbs skinnier in the morning!