You are currently viewing Python’s ABC: Understanding the Basics of Abstract Base Classes

Python’s ABC: Understanding the Basics of Abstract Base Classes

What is the ABC of Python? It stands for the abstract base class and is a concept in Python classes based on abstraction. Abstraction is an integral part of object-oriented programming.

Abstraction is what we call hiding the internal process of the program from the users. Take the example of the computer mouse where we click the left or right button and something respective of it happens or scroll the mouse wheel and a specific task happens. We are unaware of the internal functionality but we do know that clicking this button will do our job.

In abstraction, users are unaware of the internal functionality but are familiar with the purpose of the method. If we take an example of a datetime module, we do know that running the datetime.now() function will return the current date and time but are unaware of how this happens.

ABC in Python

Python is not a fully object-oriented programming language but it supports the features like abstract classes and abstraction. We cannot create abstract classes directly in Python, so Python provides a module called abc that provides the infrastructure for defining the base of Abstract Base Classes(ABC).

What are abstract base classes? They provide a blueprint for concrete classes. They are just defined but not implemented rather they require subclasses for implementation.

Defining ABC

Let’s understand with an example how we can define an abstract class with an abstract method inside it.

The class Friend derived from the ABC class from the abc module that makes it an abstract class and then within the class, a decorator @abstractmethod is defined to indicate that the function role is an abstract method.

abc module has another class ABCMeta which is used to create abstract classes.

ABC in action

Now we’ll see how to make an abstract class and abstract method and then implement it inside the concrete classes through inheritance.

In the above code, we created an abstract class called Friends and defined the abstract method role within the class using the @abstractmethod decorator.

Then we created three concrete derived classes, SachinRishu, and Yashwant, that inherits from the class Friends and implemented the abstract method role within them.

Then we instantiated the derived classes and called the role using the instance of the classes to display the result.

As we discussed earlier, abstract classes provide a blueprint for implementing methods into concrete subclasses.

In the above code, we created a blueprint for the class Details. Then we created a class called Sachin that inherits from Details, and we implemented the methods according to the blueprint.

Then we instantiated the class Sachin and then printed the values of getnamegetrole and gethobby.

What if we create a class that doesn’t follow the abstract class blueprint?

Python will raise an error upon executing the above code because the class Sachin doesn’t follow the class Details blueprint.

Use of ABC

As we saw in the above example that if a derived class doesn’t follow the blueprint of the abstract class, then the error will be raised.

That’s where ABC(Abstract Base Class) plays an important role in making sure that the subclasses must follow that blueprint. Thus we can say that the subclasses inherited from the abstract class must follow the same structure and implements the abstract methods.

Concrete methods inside ABC

We can also define concrete methods within the abstract classes. The concrete method is the normal method that has a complete definition.

In the above code, concrete_method is a concrete method defined within the abstract class and the method was invoked using the instance of the class Test.

Abstract class instantiation

An abstract class can only be defined but cannot be instantiated because of the fact that they are not a concrete class. Python doesn’t allow creating objects for abstract classes because there is no actual implementation to invoke rather they require subclasses for implementation.

Output

We got the error stating that we cannot instantiate the abstract class Details with abstract methods called getname and getrole.

Abstract property

Just as the abc module allows us to define abstract methods using the @abstractmethod decorator, it also allows us to define abstract properties using the @abstractproperty decorator.

We created an abstract class Hero and defined two abstract properties called hero_name and reel_name using @abstractproperty. Then, within the derived class RDJ, we used the @property decorator to implement them that will make them a getter method.

Then we instantiated the class RDJ and used the class instance to access the values of hero_name and reel_name.

If we had not placed the @property decorator inside the class RDJ, we would have had to call the hero_name and reel_name.

Note: abstractproperty is a deprecated class, instead we can use @property with @abstractmethod to define an abstract propertyPycharm IDE gives a warning upon using the abstractproperty class.

Deprecation warning

The above code will look like the following if we modify it.

Just apply the modifications as shown in the above code within the abstract class and run the code. The code will run without any error as earlier.

Conclusion

We’ve covered the fundamentals of abstract classes, abstract methods, and abstract properties in this article. Python has an abc module that provides infrastructure for defining abstract base classes.

The ABC class from the abc module can be used to create an abstract class. ABC is a helper class that has ABCMeta as its metaclass, and we can also define abstract classes by passing the metaclass keyword and using ABCMeta. The only difference between the two classes is that ABCMeta has additional functionality.

After creating the abstract class, we used the @abstractmethod and (@property with @abstractmethod) decorators to define the abstract methods and abstract properties within the class.

To understand the theory, we’ve coded the examples alongside the explanation.


🏆Other articles you might be interested in if you liked this one

What are class inheritance and different types of inheritance in Python?

Learn the use cases of the super() function in Python classes.

How underscores modify accessing the attributes and methods in Python.

What are __init__ and __call__ in Python.

Implement a custom deep learning model into the Flask app for image recognition.

Train a custom deep learning model using the transfer learning technique.

How to augment data using the existing data in Python.


That’s all for now

Keep Coding✌✌