Understanding abstraction in Python

| | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

In this tutorial, we will discuss the concept of abstraction in Python for an object-oriented programming approach.

Essentially, Abstraction focuses on hiding the internal implementations of a process or method from the user. Thus, the user knows what he is doing, but does not know how the work is being done.

 abstraction



& nbsp;

What is abstraction in Python?

For example, people don’t think of a car as a collection of thousands of individual parts. Instead, they see it as a well-defined object with its own unique behavior. This abstraction allows people to drive a car without being aware of the complexity of the parts that make it up. They can ignore the details of the engine transmission and braking systems. Instead, they can use the whole object.

A powerful way to manipulate abstraction &use of hierarchical classification. This allows us to layer the semantics of complex systems, breaking them down into more manageable pieces. Outside, the car is a single object. Once inside, you see that the car consists of several subsystems: steering, brakes, audio system, seat belts, etc. In turn, each of these subsystems consists of smaller blocks.

The point is that we drive a car (or any other complex system) using hierarchical abstractions.

This can also be applied to computer programs using OOP concepts. This is the essence of Object Oriented Programming.

Abstract Classes and Methods in Python

To declare an abstract class, we first need to import the abc module. Let’s take a look at an example.

 from abc import ABC class abs_class (ABC): #abstract methods 

Here abs_class &it is an abstract class within which abstract methods or any other methods can be defined.



As a property abstract classes can have any number of abstract methods, coexisting with any number of other methods. For example, we can see below.

 from abc import ABC, abstractmethod class abs_class (ABC): #normal method def method (self): # method definition @abstractmethod def Abs_method (self): #Abs_method definition 

Here method () &normal method, whereas Abs_method () &an abstract method that implements the @abstractmethod from the abc module.

Example

Now that we know about abstract classes and methods, let’s take a look at an example.

 from abc import ABC, abstractmethod class Absclass (ABC): def print (self, x): print ("Passed value:", x) @abstractmethod def task (self): print ("We are inside Absclass task") class test_class (Absclass): def task (self): print ("We are inside test_class task") class example_class (Absclass): def task (self ): print ("We are inside example_class task") #object of test_class created test_obj = test_class () test_obj.task () test_obj.print (100) #object of example_class created ex ample_obj = example_class () example_obj.task () example_obj .print (200) print ("test_obj is instance of Absclass?", isinstance (test_obj, Absclass)) print ("example_obj is instance of Absclass?", isinstance (example_obj, Absclass)) 

Output:

 example

where,

Absclass &it is an abstract class inherited from the ABC class of the abc module. It contains the abstract task () method and the print () method that are visible to the user. The other two classes inherited from this abstract class are &these are test_class and example_class . Both have their own task () method (an abstract method extension).

After the user creates objects from the test_class and example_class classes and calls the task () method for both of these, the hidden task () method definitions within both classes come into play. These definitions are hidden from the user. The abstract method task () from the abstract class Absclass is never actually called.

But when the print () method is called like for test_obj and example_obj, the print () Absclass method is called because it is not an abstract method.

Note: we cannot instantiate an abstract class. This causes Error.



👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Understanding abstraction in Python, check other abc Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Olivia Jackson

Warsaw | 2023-03-30

Maybe there are another answers? What Understanding abstraction in Python exactly means?. I just hope that will not emerge anymore

Manuel Porretti

Munchen | 2023-03-30

Maybe there are another answers? What Understanding abstraction in Python exactly means?. I am just not quite sure it is the best method

Ken Robinson

Texas | 2023-03-30

Simply put and clear. Thank you for sharing. Understanding abstraction in Python and other issues with Sync PHP module was always my weak point 😁. Will use it in my bachelor thesis

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically