---
title: "What is meant by abstraction in python?Give suitable example."  
description: "What is meant by abstraction in python?Give suitable example."  
author: "Amrita Bhattacharjee"  
published: 2023-03-28  
updated: 2023-04-19  
canonical: https://www.mindstick.com/forum/157639/what-is-meant-by-abstraction-in-python-give-suitable-example  
category: "python"  
tags: ["python"]  
reading_time: 3 minutes  

---

# What is meant by abstraction in python?Give suitable example.

What is meant by [abstraction](https://www.mindstick.com/articles/944/abstraction-in-c-sharp) in [python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?Give suitable example.

## Replies

### Reply by Aryan Kumar

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.

```python
from abc import ABC, abstractmethod
class Animal(ABC):
   def __init__(self, name):
       self.name = name

   @abstractmethod
   def speak(self):
       pass

class Dog(Animal):
   def speak(self):
       return "Woof!"
class Cat(Animal):
   def speak(self):
       return "Meow"
my_dog = Dog("Rufus")
print(my_dog.name)   # Output: Rufus
print(my_dog.speak())   # Output: Woof!
my_cat = Cat("Fluffy")
print(my_cat.name)   # Output: Fluffy
print(my_cat.speak())   # Output: Meow
```

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.

### Reply by Krishnapriya Rajeev

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:

```plaintext
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:

```plaintext
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()
```


---

Original Source: https://www.mindstick.com/forum/157639/what-is-meant-by-abstraction-in-python-give-suitable-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
