👻 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.
& 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:
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:
- Italiano Understanding abstraction in Python
- Deutsch Understanding abstraction in Python
- Français Understanding abstraction in Python
- Español Understanding abstraction in Python
- Türk Understanding abstraction in Python
- Русский Understanding abstraction in Python
- Português Understanding abstraction in Python
- Polski Understanding abstraction in Python
- Nederlandse Understanding abstraction in Python
- 中文 Understanding abstraction in Python
- 한국어 Understanding abstraction in Python
- 日本語 Understanding abstraction in Python
- हिन्दी Understanding abstraction in Python
Warsaw | 2023-03-30
Maybe there are another answers? What Understanding abstraction in Python exactly means?. I just hope that will not emerge anymore
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
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