class method in python

Class method in python– This is the advance feature of object oriented programming.Python has a class method. there are two type class method are available in python..

  1.  Static class method
  2.  Class Method

Static methods-:  Like any other languages as Java we can invoke static methods even though no instance of that class has been created, although we can call them by using a class instance. To create a static method we use the @staticmethod decorator as like this way

"""circle module: contains the Circle class."""
class Circle:
"""Circle class"""
all_circles = []                                         1
pi = 3.14159
def __init__(self, r=1):
"""Create a Circle with the given radius"""
self.radius = r
self.__class__.all_circles.append(self)                  2
def area(self):
"""determine the area of the Circle"""
return self.__class__.pi * self.radius * self.radius

@staticmethod
def total_area():
"""Static method to total the areas of all Circles """
total = 0
for c in Circle.all_circles:
total = total + c.area()
return total
  • Class variable containing list of all circles that have been created
  • When an instance is initialized it adds itself to the all_circles list.

 

Class methods in python-: Class methods are similar to static methods. It can be invoked before an object of the class has been instantiated or by using an instance of the class. But class methods are implicitly passed the class they belong to as their first parameter so we can code them more simply as like this way….

"""circle_cm module: contains the Circle class."""
class Circle:
    """Circle class"""
    all_circles = []                                          1
    pi = 3.14159
    def __init__(self, r=1):
        """Create a Circle with the given radius"""
        self.radius = r
        self.__class__.all_circles.append(self)
    def area(self):
        """determine the area of the Circle"""
        return self.__class__.pi * self.radius * self.radius

    @classmethod                                             2
    def total_area(cls):                                     3
        total = 0
        for c in cls.all_circles:                            4
            total = total + c.area()
        return total
>>> import circle_cm
>>> c1 = circle_cm.Circle(1)
>>> c2 = circle_cm.Circle(2)
>>> circle_cm.Circle.total_area()
15.70795
>>> c2.radius = 3
>>> circle_cm.Circle.total_area()
31.415899999999997
  • Variable containing list of all circles that have been created

The @classmethod decorator is used before the method def 2. The class parameter is traditionally cls 3. We can use cls instead of self.__class__ 4.

By using a class method instead of a static method We do not have to hard code the class name into total_area. As a result, any subclasses of Circle can still call total_area and refer to their own members, not those in Circle.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *