Wednesday, April 22, 2020

abc — Abstract Base Classes

What is Abstract Base Class:

If Parent classes are concrete classes, then there are chances the it may get instantiated.
So if we don't want parent classes to be instantiated, then we need to make our parent class as Abstract class.

The abstract class will serve as template for the child class;

How to Create Abstract Base Class


from abc import ABC

class Employee(ABC):
    pass


Here abc is module and ABC is class.

We have derived our Employee Class from Abstract Base Class name ABC.


Below is the sample code construct to create the ABC

from abc import ABC
from abc import abstractmethod

class employee(ABC):
  @abstractmethod
  def m1(self):
    print("I am M1 from employee")

  def m2(self):
    print("I am m2 from employee")

class manager(employee):

  def m1(self):
    employee.m1(self)
    print("I am m1 from manager")


In this code construct, we cannot instantiate the employee class. We can only instantiate manager class.

When trying to instantiate employee class we get below error.

>>> o=employee()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    o=employee()
TypeError: Can't instantiate abstract class employee with abstract methods m1


We are able to instantiate manager class.

>>> o=manager()
>>> o.m1()
I am m1 from manager

We can also call Parent class's abstract method from child class using employee.m1(self)

>>> o=manager()
>>> o.m1()
I am M1 from employee

I am m1 from manager


Test Cases:

1) Parent Class is derived from ABC, but parent class doesn't have abstractmethod.


from abc import ABC

class employee(ABC):

  def m1(self):#This is not abstract method
    print("I am M1 from employee")

  def m2(self):
    print("I am m2 from employee")

class manager(employee):
  
  def m1(self):
    print("I am m1 from manager")

In this case we still be able to instantiate employee class and also manager class.
This is as good as simple single inheritance concept.

1) Parent class derived from ABC and has abstractmethod in it. 
from abc import ABC

class employee(ABC):
  @abstractmethod #This is abstract method 
  def m1(self):#This is not abstract method
    print("I am M1 from employee")

  def m2(self):
    print("I am m2 from employee")

class manager(employee):
  
  def m1(self):
    print("I am m1 from manager")

In this case we cannot instantiate employee class.

>>> o=employee()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    o=employee()
TypeError: Can't instantiate abstract class employee with abstract methods m1

2) Parent class derived from ABC and has abstractmethod in it. Child class dont have m1 implemented in it.
from abc import ABC

class employee(ABC):
  @abstractmethod #This is abstract method 
  def m1(self):#This is not abstract method
    print("I am M1 from employee")

  def m2(self):
    print("I am m2 from employee")

class manager(employee):
  
   pass



>>> o=manager()
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    o=manager()
TypeError: Can't instantiate abstract class manager with abstract methods m1

Here you cannot create object of manager.
This means that in the child class, if we don't implement the  override method , then we cannot instantiate the child class.






No comments:

Post a Comment