Python Programming Using Problem Solving by Harsh Bhasin
>>>
============ RUN C:/Python/Class/Constructor1.py ===============
Enter nameââ:Harsh
Enter age:28
Nameâ: Harsh
Ageâ: 28
Name: ABC
Age: 20
>>>
A parameterized constructor is one that takes arguments, for example, in the following code, the parameterized constructor, which takes two parameters name and age has been created. In order to assign the values to the Object, the instantiation must be of the form:
e2=employee('Naved', 32)
Note that while defining the parameterized __init__, the first parameter is always âself,â the rest of the parameters are the values to be assigned to different data members of the class. In the case of employee class, three parameters, âself,â âname,â and âageâ are given.
Code:
>>>
class employee:
âââdef getdata(self):
ââââââself.name=input('Enter name :')
ââââââself.age=input('Enter age :')
âââdef putdata(self):
ââââââprint('Name :',self.name)
ââââââprint('Age :',self.age)
âââdef __init__(self, name, age):
ââââself.name=name
ââââself.age=age
âââdef __del__():
ââââââprint('Done')