Python constructor
Python constructor
In C++ or Java, the constructor has the same name as its class, but it treats
constructor differently in Python. It is used to create an object.
1. Parameterized Constructor
2. Non-parameterized Constructor
Example
1. class Employee:
2. def __init__(self, name, id):
3. self.id = id
4. self.name = name
5.
6. def display(self):
7. print("ID: %d \nName: %s" % (self.id, self.name))
8.
9.
10. emp1 = Employee("John", 101)
11. emp2 = Employee("David", 102)
12.
13. # accessing display() method to print employee 1 information
14.
15. emp1.display()
16.
17. # accessing display() method to print employee 2 information
18. emp2.display()
Output:
ID: 101
Name: John
ID: 102
Name: David
Counting the number of objects of a class
The constructor is called automatically when we create the object of the
class. Consider the following example.
Example
1. class Student:
2. count = 0
3. def __init__(self):
4. Student.count = Student.count + 1
5. s1=Student()
6. s2=Student()
7. s3=Student()
8. print("The number of students:",Student.count)
Output:
ADVERTISEMENT
Example
1. class Student:
2. # Constructor - non parameterized
3. def __init__(self):
4. print("This is non parametrized constructor")
5. def show(self,name):
6. print("Hello",name)
7. student = Student()
8. student.show("John")
Example
1. class Student:
2. # Constructor - parameterized
3. def __init__(self, name):
4. print("This is parametrized constructor")
5. self.name = name
6. def show(self):
7. print("Hello",self.name)
8. student = Student("John")
9. student.show()
Output:
Example
1. class Student:
2. roll_num = 101
3. name = "Joseph"
4.
5. def display(self):
6. print(self.roll_num,self.name)
7.
8. st = Student()
9. st.display()
Output:
101 Joseph
Example
1. class Student:
2. def __init__(self):
3. print("The First Constructor")
4. def __init__(self):
5. print("The second contructor")
6.
7. st = Student()
Output:
In the above code, the object st called the second constructor whereas both
have the same configuration. The first method is not accessible by
the st object. Internally, the object of the class will always call the last
constructor if the class has multiple constructors.
SN Function Description
Example
1. class Student:
2. def __init__(self, name, id, age):
3. self.name = name
4. self.id = id
5. self.age = age
6.
7. # creates the object of the class Student
8. s = Student("John", 101, 22)
9.
10. # prints the attribute name of the object s
11. print(getattr(s, 'name'))
12.
13. # reset the value of attribute age to 23
14. setattr(s, "age", 23)
15.
16. # prints the modified value of age
17. print(getattr(s, 'age'))
18.
19. # prints true if the student contains the attribute with name id
20.
21. print(hasattr(s, 'id'))
22. # deletes the attribute age
23. delattr(s, 'age')
24.
25. # this will give an error since the attribute age has been deleted
26. print(s.age)
Output:
John
23
True
AttributeError: 'Student' object has no attribute 'age'
SN Attribute Description
Example
1. class Student:
2. def __init__(self,name,id,age):
3. self.name = name;
4. self.id = id;
5. self.age = age
6. def display_details(self):
7. print("Name:%s, ID:%d, age:%d"%(self.name,self.id))
8. s = Student("John",101,22)
9. print(s.__doc__)
10. print(s.__dict__)
11. print(s.__module__)
Output:
None
{'name': 'John', 'id': 101, 'age': 22}
__main__