Built-in Abstract classes

Python 3 standard library provides a few built-in abstract classes for both abstract and non-abstract methods. These include sequence, mutable sequence, iterable, and so on. It often serves as an alternative to subclassing a built-in Python class. For example, subclassing the MutableSequence can substitute the subclassing of list or str. The main purpose of using Abstract class is that it allows you to consider a common type of collection rather than coding for each type of collection. Here we will discuss Single-Method ABCs and Alternative-Collection ABCs.

  • Single-Method ABCs
  • Alternative-Collection ABCs

Single-Method ABCs

Python has five abstract base classes. They are as follows:

  • Callable (__call__)
  • Container (__contains__)
  • Hashable (__hash__)
  • Iterable (__iter__)
  • Sized (__len__)

These abstract base classes contain one abstract method each. Let’s consider an example of the __len__ method.

Python3




from collections.abc import Sized
  
  
class SingleMethod(object):
    def __len__(self):
        return 10
  
  
print(issubclass(SingleMethod, Sized))


Output:

True

Any class that has the appropriate method is considered as the subclass of the abstract base class. Out of the above five abstract base classes, the Iterator is slightly different. It provides an implementation for __iter__ and adds an abstract method called __next__.

Alternative-Collection ABCs

Alternative-Collection ABCs are built-in abstract base classes that identify subclasses, which serve similar purposes. They can be divided into three categories. Let’s go through one by one.

  • Sequence and Mutable Sequence: Sequence and Mutable Sequence are abstract base classes that generally behaves like tuples or list. A sequence abstract  base class requires __getitem__ and __len__ , whereas mutable sequence needs __setitem__ and __getitem__.
  • Mapping: Mapping comes with mutable mapping, which is mainly for dictionary-like objects
  • Set: The set comes with a mutable set that is intended for unordered collections.

Abstract Base Class (abc) in Python

Have you ever thought about checking whether the objects you are using adheres to a particular specification? It is necessary to verify whether an object implements a given method or property, especially while creating a library where other developers make use of it. A developer can use hasattr or isinstance methods to check whether the input conforms to a particular identity. But sometimes it is inconvenient to use those methods to check a myriad of different properties and methods.    

As a solution to this inconvenience, Python introduced a concept called abstract base class (abc). In this section, we will discuss the abstract base class and its importance.  

  • Abstract Base Class
  • Declaring an Abstract Base Class
  • Why declare an Abstract Base Class?
  • Abstract Properties
  • Built-In Abstract Classes

Similar Reads

Abstract Base Class

The main goal of the abstract base class is to provide a standardized way to test whether an object adheres to a given specification. It can also prevent any attempt to instantiate a subclass that doesn’t override a particular method in the superclass. And finally, using an abstract class, a class can derive identity from another class without any object inheritance....

Declaring an Abstract Base Class

Python has a module called abc (abstract base class) that offers the necessary tools for crafting an abstract base class. First and foremost, you should understand the ABCMeta metaclass provided by the abstract base class. The rule is every abstract class must use ABCMeta metaclass....

Why Declare an Abstract Base Class?

...

Abstract Properties

...

Built-in Abstract classes

To understand the need to declare a virtual subclass, we need to consider the example of a list-like object where you don’t want to put a restriction of only considering list or tuple. Before that let’s see how to use isinstance to check against a list or tuple of class....

Summary

...

Contact Us