Python Abstract Property

This tutorial will discuss using the abc
or abstract base classes module to create classes with abstract properties in Python.
Python Abstract Property
Abstraction in object-oriented programming is used to hide unnecessary information from users. The inner working of a function will be hidden from the user, but the user can use the function to perform a task.
For example, we use computer software to perform different tasks, but we don’t know how the software performs the task. We only give inputs to the software and get outputs from it.
We can use abstraction in Python to reduce the complexity of a program by hiding irrelevant information.
A class will become abstract if it contains one or more abstract methods. Abstract methods are defined in a subclass, and the abstract class will be inherited from the subclass because abstract classes are blueprints of other classes.
In Python, we can use the abc
module or abstract base classes module to implement abstract classes. The abstract base class provides an interface for subclasses, and it is beneficial when the code is large and remembering all the classes is challenging.
We need to import the abc
module in the base class and use it to decorate the base class method as an abstract. To define an abstract method, we can use the @abstractmethod
decorator before defining the method in the base class, and we can use the @property
decorator to make a method an abstract property.
For example, let’s import the abc
module and create a Bike
class that will be inherited from the ABC class. Inside the Bike
class, we will create an abstract method, mileage()
.
We can now inherit the base class to implement different subclasses with abstract methods. See the code below.
# Class Code
from abc import ABC, abstractmethod
class Bike(ABC):
@property
@abstractmethod
def mileage(self):
pass
class Honda(Bike):
def mileage(self):
print("The mileage is 20kmph")
class CD70(Bike):
def mileage(self):
print("The mileage is 15kmph ")
class CD150(Bike):
def mileage(self):
print("The mileage is 34kmph ")
# Main Code
h = Honda()
h.mileage()
r = CD70()
r.mileage()
s = CD150()
s.mileage()
Output:
The mileage is 20kmph
The mileage is 15kmph
The mileage is 34kmph
In the above code, we created objects of the subclasses, and then we called their method mileage()
, which will print the mileage of the bike. The Bike
class also has the mileage()
method, but we cannot call it because the method is abstract.
For example, let’s repeat the above example and call the mileage
method of the Bike
class. See the code below.
# Class Code
from abc import ABC, abstractmethod
class Bike(ABC):
@property
@abstractmethod
def mileage(self):
pass
class Honda(Bike):
def mileage(self):
print("The mileage is 20kmph")
class CD70(Bike):
def mileage(self):
print("The mileage is 15kmph ")
class CD150(Bike):
def mileage(self):
print("The mileage is 34kmph ")
# Main Code
b = Bike()
b.mileage()
Output:
TypeError: Can't instantiate abstract class Bike with abstract methods mileage
The above code clearly says that the abstract class Bike
has abstract methods, which is why its object cannot be created, and its methods cannot be called.
We can also make methods of other classes as abstract methods using the @abstractmethod
decorator before the definition of the first method. When one tries to make an object of that class to access the methods, Python will give an error.
For example, let’s make the methods of a subclass abstract. See the code below.
# Class Code
from abc import ABC, abstractmethod
class Bike(ABC):
@property
@abstractmethod
def mileage(self):
pass
class Honda(Bike):
@abstractmethod
def mileage(self):
print("The mileage is 20kmph")
def mileage2(self):
print("The mileage is 200 kmph")
# Main Code
b = Honda()
b.mileage2()
Output:
TypeError: Can't instantiate abstract class Honda with abstract methods mileage
We have defined two methods in the Honda
class, but none of them is accessible because the class is abstract. Check this link for more details about the abstract base classes.
Related Article - Python Class
- Python Generator Class
- Data Class Inheritance in Python
- Serialize a Python Class Object to JSON
- Python Class Factory
- Python Class Equality