An Introduction to Python’s Abstract Class
It takes subclasses of an Abstract Class to use its abstract methods, hence it is not possible to build Abstract Classes on its own. At this point, we introduce the idea of inheritance for the abstract class, which allows us to derive the object from the base class. Each Python abstract class has its own set of properties for its abstract method, denoted by the @abstractproperty keyword.
The Abstract class may hold both the abstract and concrete classes. Using an abstract class allows us to build a generic method structure without providing concrete examples of each methods. Nevertheless, it is possible to specify abstract methods with implementations in the abstract class; in this case, each subclass that derives from the abstract class will be responsible for providing the implementation of the corresponding abstract method. The resulting class will throw an exception if it does not implement all of the abstract methods. While we can’t directly construct an instance of an abstract class, we may utilise one to offer functionality to a base class or child class by instantiating an instance of that class.
Usefulness of Abstract Groups
One of its primary roles is to implement the basic classes’ expected behaviour.
Second, it establishes a standard application programming interface (API) for all of the subclasses, which is very helpful when a third party develops plugins for the application.
Three, it’s tough to keep track of numerous classes in a long piece of code, so this was a great aid. Syntax
From abc import ABC
Class Educba(ABC):
Any class that does not inherit the ABC metaclass from Python’s abc module is not an abstract class. Specifically, the abc module pulls in the ABC metaclass.
Python’s Abstract Functions
Methods that are defined but not implemented are considered abstract. Syntax
from abc import ABC, abstractmethod
Class Educba(ABC):
@abstractmethod
def mymethod(self):
#empty body
pass
Decorating a method with the term @abstractmethod establishes its status as an abstract method inside the context of an abstract class. The @abstractmethod decorator requires the abc library, which is part of standard Python distribution.
How did Python’s Abstract Classes operate? Python does not have built-in support for abstract classes, however there is a library module with the name “ABC” that provides the foundation for building abstract classes. A concrete class is generated to serve as an implementation of the abstract base class once the method of the base class has been marked as abstract. With the use of the decorator term @abstractmethod, a method may be converted into an abstract method.
The abstract classes may be generated with the help of the ABC module, and the @abstractmethod decorator can be used to mark a method as abstract. The agreement between the abstract and concrete classes is formalised via the ABC module.
Python’s abc module serves as the framework for writing ABC definitions. Several concrete classes that extend ABC may be found in the collections module and are further subdivided. In addition to these features, the collections module also includes several abstract base classes (ABC) that may be used to verify if a class or instance implements a given interface.
The next category is included with this module:
class abc.ABCMeta
Specifically, Abstract Base Classes may be defined with the help of the metaclass (ABC)
To create an abstract foundation, a metaclass is used.
class.
A from abc import ABCMeta
Useful
class C:
__metaclass__ = ABCMeta
MyABC.register(tuple)
assert issubclass(tuple, C)
assert isinstance((), C)
obtained # importing the ABC module
output
from abc import ABC, abstractmethod
class Shape(ABC):
def common(self):
print("This is a concrete method")
@abstractmethod # decorator
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Square(Shape):
def __init__(self,side):
self.__side=side
def area(self):
return self.__side*self.__side
def perimeter(self):
return 4*self.__side
class Rectangle(Shape):
def __init__(self,length,breath):
def __init__(self):
return self.__length*self.__breath
def perimeter(self.__length+self.__breath)
S1=Square(4)
print(S1.common())
print(S1.area())
print(S1.perimeter())
R1=Rectangle(2,4)
print(R1.common())
print(R1.area())
print(R1.perimeter())
Using this approach provides a tangible result.
16
16
Here, we have a real-world strategy.
8
12
One concrete method, “common,” and two abstract methods, “area” and “perimeter,” make up the Abstract class, Shape, seen above. Both the Square and Rectangle classes are descended from the abstract Shape class and hence are responsible for enforcing the implementation of the abstract method.
Effectiveness via
Subclassing:
import abc
class Shape:
def area(self):
pass
class Square(Shape):
def area(self):
print("Square is a child class")
print( issubclass(Square,Shape))
print( isinstance(Square(), Shape))
Qualities that are Abstraction-Based The @abstractproperty annotation is used to declare abstract properties, which are shared by abstract classes’ abstract methods.
In order to work with abstract classes, we may now make use of property, property.getter(), property.setter(), and property.deleter(). Syntax
cclass class_name(ABC):
ass="token keyword">def getx(self): ...
def setx(self, value): ...
x = abstractproperty(getx, setx)
Python
2
class C(ABC):
@property
@abstractmethod
def my_abstract_property(self):
Python
3.3
Result of import abc
:
from abc import ABC, abstractmethod
class Shape(ABC):
@abc.abstractproperty
def area(self):
return "Shape class"
class Square(parent):
@property
def area(self):
return "Square class"
try:
s1 =Shape()
print( s1.area)
except Exception as err:
print (err)
s1 = Square()
print (s1.area)
Unable to create instance of abstract class Area in the form of an abstract technique
Classe carrée