Photo by Chris Ried on Unsplash
Now, it is time to learn about function scope. Based on the scope, functions/methods are of two types.
They are:
- Class methods
- Static methods
class Circle(object): no_of_circles = 0
def __init__(self, radius): self.__radius = radius Circle.no_of_circles += 1
@classmethod def getCirclesCount(cls): return cls.no_of_circles
@staticmethod def square(x): return x ** 2
def area(self): return 3.14 * self.square(self.__radius)
c1 = Circle(3.9)print(c1.area()) # 47.7594# Calling Class Object Methodprint(c1.square(10)) # 100
c2 = Circle(5.2)c3 = Circle(4.8)
print(c1.getCirclesCount()) # 3print(c2.getCirclesCount()) # 3print(c3.getCirclesCount()) # 3
# calling Class methodprint(Circle.getCirclesCount()) # 3
# Calling Static methodprint(Circle.square(10)) # 100
Guess the Output of the following
class A:
# No Effect @staticmethod def m1(self): print('Static Method')
@classmethod def m1(self): print('Class Method')
A.m1() # Class MethodA().m1() # Class Method
An Abstract Base Class or ABC mandates the derived classes to implement specific methods from the base class.
It is not possible to create an object from a defined ABC class but creating objects of derived classes is possible when derived classes override existing functionality of all abstract methods defined in an ABC class.
from abc import ABC, abstractmethod
class Shape(ABC): @abstractmethod def area(self): pass
@abstractmethod def perimeter(self): pass
# s1 = Shape()# Error. We cannot create an oject for abstract class.
class Circle(Shape): def __init__(self, radius): self.__radius = radius
@staticmethod def square(x): return x ** 2
def area(self): return 3.14 * self.square(self.__radius)
def perimeter(self): return 2 * 3.14 * self.__radius
c1 = Circle(5)print(c1.area()) # 78.5print(c1.perimeter()) # 31.40
Scenario - Abstract class and Multiple Class method Annotations
from abc import ABC, abstractmethod
class A(ABC):
@classmethod @abstractmethod def m1(self): print('In abstract base class A, Method m1.')
class B(A):
@classmethod def m1(self): print('In base class B, Method m1.')
b = B()b.m1()B.m1()A.m1()"""OutputIn base class B, Method m1.In base class B, Method m1.In abstract base class A, Method m1."""
Multiple Inheritance
from abc import ABC, abstractmethod
# Abstract Base classclass A(ABC):
@abstractmethod def m1(self): print('In abstract base class A, Method m1.')
# Base classclass B(A):
# Function Override def m1(self): print('In base class B, Method m1.')
# Derived Classclass C(B):
def m2(self): print('In derived class C, Method m2.')
c = C()c.m1()c.m2()
# Output"""In base class B, Method m1.In derived class C, Method m2."""
Abstract Class and Derived Class example
import inspect
from abc import ABC, abstractmethod
# Define the abstract class 'Animal' below# with abstract method 'say'class Animal(ABC): @abstractmethod def say(self): pass
# Define class Dog derived from Animal# Also define 'say' method inside 'Dog' classclass Dog(Animal): def say(self): return "I speak Booooo"
if __name__ == '__main__':
if issubclass(Animal, ABC): print("'Animal' is an abstract class")
if '@abstractmethod' in inspect.getsource(Animal.say): print("'say' is an abstract method")
if issubclass(Dog, Animal): print("'Dog' is dervied from 'Animal' class")
d1 = Dog() print("Dog,'d1', says :", d1.say())
"""'Animal' is an abstract class'say' is an abstract method'Dog' is dervied from 'Animal' classDog,'d1', says : I speak Booooo"""
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod def m1(self): print('In class A, Method m1.')
class B(A):
@staticmethod def m1(self): print('In class B, Method m1.')
b = B()B.m1(b)
# In class B, Method m1.
def s1(x, y): return x * y
class A:
@staticmethod def s1(x, y): return x + y
def s2(self, x, y): return s1(x, y)
a = A()print(a.s1(3, 7)) # 10print(a.s2(3, 7)) # 21