Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

Abstract class in Python

Abstraction is an important concept of OOP. It is basically used to hide some internal functionality of the method from the end-user. The users are only concerned with 'what a method can do?' rather than how they are implemented. So, abstraction is the technique of hiding unnecessary information from the user.

Taking a simple example, we all use a smartphone, we can make a call via smartphone. Here, we know a smartphone can make a call but How the ultimate procedure acts at the backend is unknown to us and also we don't need it too. This is how the concept of abstraction works in OOP.

The method inside a class having only declaration but not the definition is called the abstract method. The class having such a type of method is known as abstract class.

Unlike in C#, C++, java, In python by default, there's is no concept of abstraction. To implement abstraction in python, we need to import a module ABC ("Abstract Base Class") and an abstract method decorator.

Note: abstract classes cannot be instantiated.


from abc import ABC, abstractmethod

class Pen(ABC):
    @abstractmethod
    def write():
        print("I'm writing something.")

pen1=pen()
pen1.write() 
Output

TypeError: Can't instantiate abstract class pen with abstract method write
 

Even if we inherit the class pen with the sub-class sign pen and try to instantiate the sub-class sign pen, there also we get the same error.


from abc import ABC, abstractmethod

class Pen(ABC):

    @abstractmethod
    def write(self):
        pass

class SignPen(Pen):
    pass

pen1=SignPen()
    
Output

TypeError: Can't instantiate abstract class sign pen with abstract method write

So, we need to define that abstract_class in sub-class otherwise, this sub-class is also an abstract class because it is inheriting abstract class pen. After defining only we can instantiate it.

     
from abc import ABC, abstractmethod

class Pen(ABC):
    @abstractmethod
    def write(self):
        pass

class SignPen(pen):
    def write(self):
        print("Signpen is working.")

pen1=SignPen()
pen1.write()
    
Output

Signpen is working.
    
Why use Abstract Base Classes?

By defining an abstract base class, you can define a common Application Program Interface(API) for a set of subclasses.

This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins

but can also help you when working in a large team or with a large code-base where keeping all classes in your mind is difficult or not possible.