Class Method Decorator in Python


This program defines a Python class "student" with the following attributes and methods:

  • count attribute: This is a class attribute that is shared by all objects of the class. It is initialized to 0.
  • __init__ method: This method is called when an object of the class is created. It initializes the name and age attributes with the passed values, and increments the count class attribute by 1 each time a new object is created.
  • printDetail method: This method prints the values of the name and age attributes.
  • total method: This method is a class method and returns the value of the count attribute, which keeps track of the total number of objects created.

The program then creates two objects o and a of the "student" class, passing the respective name and age as arguments. The printDetail method is then called on each object to print their details. Finally, the value of the count attribute is accessed using both the class name and an object reference, which returns the same result.


Source Code

class student:
    count = 0
 
    def __init__(self, name, age):
        self.name = name
        self.age = age
        student.count += 1
 
    def printDetail(self):
        print("Name  : ", self.name, "  Age : ", self.age)
 
    @classmethod
    def total(cls):
        return cls.count
 
 
o = student("Joes", 25)
o.printDetail()
a = student("Raja", 45)
a.printDetail()
 
print("Total Admission :", student.total())
print("Total Admission :", o.total())
 
To download raw file Click Here

Output

Name  :  Joes   Age :  25
Name  :  Raja   Age :  45
Total Admission : 2
Total Admission : 2

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial