Concepts - Abstract Class and methods in Python

Published on
4 mins read
––– views
Photo by Chris Ried on Unsplash
python-notes

Now, it is time to learn about function scope. Based on the scope, functions/methods are of two types.

They are:

  1. Class methods
  2. 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 Method
print(c1.square(10)) # 100
c2 = Circle(5.2)
c3 = Circle(4.8)
print(c1.getCirclesCount()) # 3
print(c2.getCirclesCount()) # 3
print(c3.getCirclesCount()) # 3
# calling Class method
print(Circle.getCirclesCount()) # 3
# Calling Static method
print(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 Method
A().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.5
print(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()
"""
Output
In 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 class
class A(ABC):
@abstractmethod
def m1(self):
print('In abstract base class A, Method m1.')
# Base class
class B(A):
# Function Override
def m1(self):
print('In base class B, Method m1.')
# Derived Class
class 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' class
class 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' class
Dog,'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)) # 10
print(a.s2(3, 7)) # 21