Python File handling

Abstraction in Python

  • In Python, a class contains lots of data and the user may not need all the data. So in this case, we can hide some unnecessary data and expose only user required data. The process of hiding the real implementation from the user and point up the only usage of it is known as abstraction.
  • For example: A car has many different parts like an engine, battery, radiator, axles, wheels, steering, and other electronic equipment. The driver of a car does not require any knowledge of the internal parts of the car, he should only know that how to drive a car. A car driver may never bother about how a car engine is designed. another example, can be tv, fan, mobile, laptop, etc.
  • The benefits of Abstraction is the user will have only data of his requirements so he will not get confused with unnecessary data.
  • Through the abstraction a developer can hide all the irrelevant data of the application to reduce complexity.
  • In python, abstraction can be done through abstraction classes

Abstract Class and Abstract Method in Python

  • A method whose action is redefined in the subclasses based on object requirements is known as the abstract method.
  • Abstract methods are written without body and their body will be defined in the subclasses.
  • An abstract class is a class that contains some abstract methods.
  • A abstract class contains abstract methods whose body is defined as subclasses so it is not possible to estimate the memory space required for the object of an abstract class.
  • So, the python virtual machine can’t create objects of an abstract class.
  • In python it is possible to write an abstract method with the body. to do so we need to mark a method as abstract using the @abstractmethod decorator.
  • When we derived abstract class into subclass we need to implement all the abstract methods with the body in the subclass.
  • To create an abstract class we need to derive meta class ABC that belong to abc (abstract class )module
Abstraction in Python
Abstraction in Python
#Abstract class & method example:

from abc import ABC, abstraction method
import math.

class MyClass (ABC):
    @abstract method
    def calculate (self, ×) :
        pass.

#subclass: 
class circle (MyClass):

    def calculate (self,×):
        print ("Area of circle = ".(math.pi * × * x))

#sub class 
class square (Myclass):

    def calculate (self,×):
        print ("Area of square = ".(×*×))

circle = circle ( )
circle. calculate (7)

square = Square ( )
square. calculate (19)
OUTPUT

Area of circle = 153.94
Area of square = 361
  • In the above example we import ABC metaclass from abc module. A metaclass ABC defines that the class which is derived from it becomes an abstract class.
  • Our MyClass is an abstract class because it is derived from ABC metaclass.
  • The MyClass has an abstract method calculate which doesn’t have body implementation.
  • In circle and square class we defined the body of an abstract method calculate as per our requirement. Which performs different tasks based on class abc.