Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on usage of it. For example, consider you have bought a new electronic gadget.
In Python, abstraction is implemented using classes and objects, where we define abstract classes and methods that can be implemented in concrete subclasses.
Abstraction in Python helps to hide the complexity of a system and allows us to work with high-level concepts instead of dealing with low-level details. This can make code easier to understand, maintain, and extend.
The Animal class provides an abstraction of the concept of an animal that can speak, without specifying how each animal speaks. This allows us to work with high-level concepts like animals and their ability to speak, without worrying about the specific implementation details of each animal's speech.
Abstraction is used to hide the internal functioning of a method from the user. They only interact with the
external implementation of the function, but the internal working is hidden.
In Python, abstraction is used to hide data that is irrelevant to the user to reduce its complexity. We can accomplish this by using abstract classes.
An abstract class consists of one or more abstract methods (methods that do not contain their implementation). An abstract class can be inherited by the subclass and the abstract method is then defined in this subclass. However, we cannot create objects for the abstract class, ie. it cannot be instantiated.
Python provides the abc module to implement abstraction.
Example:
from abc import ABC
Python does not provide the abstract class itself, unlike other high-level languages, due to which we need to import the abc module that provides the base for defining Abstract Base Classes (ABC). ABC decorates the methods of the base class as abstract. We use the abstractmethod decorator to define an abstract method (or it automatically becomes the abstract method if no definition is provided).
Example:
from abc import ABC, abstractmethod
class Class1(ABC):
def method1(self): # abstract method
pass
class Class2(Class1):
def method1(self): # implemented method
print("This method has been implemented.")
# driver code
c = Class2() # create an instance of Class2
c.method1()
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on usage of it. For example, consider you have bought a new electronic gadget.
In Python, abstraction is implemented using classes and objects, where we define abstract classes and methods that can be implemented in concrete subclasses.
Abstraction in Python helps to hide the complexity of a system and allows us to work with high-level concepts instead of dealing with low-level details. This can make code easier to understand, maintain, and extend.
The Animal class provides an abstraction of the concept of an animal that can speak, without specifying how each animal speaks. This allows us to work with high-level concepts like animals and their ability to speak, without worrying about the specific implementation details of each animal's speech.
Abstraction is used to hide the internal functioning of a method from the user. They only interact with the external implementation of the function, but the internal working is hidden.
In Python, abstraction is used to hide data that is irrelevant to the user to reduce its complexity. We can accomplish this by using abstract classes.
An abstract class consists of one or more abstract methods (methods that do not contain their implementation). An abstract class can be inherited by the subclass and the abstract method is then defined in this subclass. However, we cannot create objects for the abstract class, ie. it cannot be instantiated.
Python provides the abc module to implement abstraction.
Example:
Python does not provide the abstract class itself, unlike other high-level languages, due to which we need to import the abc module that provides the base for defining Abstract Base Classes (ABC). ABC decorates the methods of the base class as abstract. We use the abstractmethod decorator to define an abstract method (or it automatically becomes the abstract method if no definition is provided).
Example: