Python abstraction

In Object Oriented Programming (OOP) languages abstraction feature shows only essential attributes and hides unnecessary information. For example when you use google for signup to other website then google only sent the basic information of you to that site, and google hide details information.


Objectives

  • Python abstraction with example


Python abstraction with example

Codes

from abc import ABC, abstractmethod


# Person Abstract class
class Person(ABC):

    @abstractmethod
    def details(self):
        pass


# Implement Abstract class
class Customer(Person):

    def details(self):
        print("Implemented abstract method")


# Create Child class object
customer = Customer()

# Call the abstract method
customer.details()
Here
  • ABC : Abstract Base classes(ABC). By default, Python does not provide abstract classes. Python comes with a module that provides the base for defining Abstract Base classes and that module name is ABC.

  • @abstractmethod : A method becomes abstract when decorated with the keyword @abstractmethod decorator.


Python abstraction video tutorial