{"id":8495,"date":"2023-08-17T08:00:35","date_gmt":"2023-08-17T02:30:35","guid":{"rendered":"https:\/\/tutorpython.com\/?p=8495"},"modified":"2023-10-19T15:15:51","modified_gmt":"2023-10-19T09:45:51","slug":"abstraction-in-python","status":"publish","type":"post","link":"https:\/\/tutorpython.com\/abstraction-in-python","title":{"rendered":"Abstraction in Python"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">In This Article<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tutorpython.com\/abstraction-in-python\/#Abstract_Class_in_Python\" >Abstract Class in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tutorpython.com\/abstraction-in-python\/#Heres_an_example_of_using_abstraction_in_Python\" >Here&#8217;s an example of using abstraction in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tutorpython.com\/abstraction-in-python\/#Code_explanation_of_using_abstraction_in_Python\" >Code explanation of using abstraction in Python<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p><a href=\"https:\/\/tutorpython.com\/tutorial\/abstraction-in-python\">Abstraction in Python<\/a> is a fundamental concept that allows developers to create more modular and maintainable code. It is particularly relevant in <a href=\"https:\/\/tutorpython.com\/tutorial\/object-oriented-programming-in-python\" target=\"_blank\" rel=\"noopener\">object-oriented programming<\/a> languages like Python.<\/p>\n<p>In Python, abstraction is achieved through the use of abstract classes and interfaces.<\/p>\n<p>Abstraction involves separating the essential features or behaviors of an object from the specific implementation details. It provides a high-level view of an object, focusing on what it does rather than how it does it.<\/p>\n<p>This approach allows developers to work with complex systems by hiding unnecessary details and exposing only the relevant functionalities.<\/p>\n<p>In Python, abstraction is often implemented using<a href=\"https:\/\/tutorpython.com\/tutorial\/abstraction-in-python\"> abstract classes<\/a>, which cannot be instantiated directly.<\/p>\n<p>An abstract class serves as a blueprint for other <a href=\"https:\/\/tutorpython.com\/tutorial\/class-objects-in-python\" target=\"_blank\" rel=\"noopener\">classes<\/a> and provides a common interface for its subclasses. It defines a set of abstract methods that must be implemented by any concrete class <a href=\"https:\/\/tutorpython.com\/tutorial\/inheritance-in-python\" target=\"_blank\" rel=\"noopener\">inheriting<\/a> from it. Abstract methods are declared without implementation and act as placeholders for the required functionality.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Abstract_Class_in_Python\"><\/span>Abstract Class in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To create an abstract class in Python, we need to import the <code>ABC<\/code> (Abstract Base Class) module from the <code>abc<\/code> <a href=\"https:\/\/tutorpython.com\/tutorial\/modules-in-python\" target=\"_blank\" rel=\"noopener\">module<\/a> and use the <code>@abstractmethod<\/code> <a href=\"https:\/\/tutorpython.com\/tutorial\/decorators-in-python\" target=\"_blank\" rel=\"noopener\">decorator<\/a> to mark the methods as abstract.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Heres_an_example_of_using_abstraction_in_Python\"><\/span>Here&#8217;s an example of using abstraction in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-python\">from abc import ABC, abstractmethod\r\n\r\nclass Shape(ABC):\r\n    @abstractmethod\r\n    def area(self):\r\n        pass\r\n\r\n    @abstractmethod\r\n    def perimeter(self):\r\n        pass\r\n\r\nclass Rectangle(Shape):\r\n    def __init__(self, length, width):\r\n        self.length = length\r\n        self.width = width\r\n\r\n    def area(self):\r\n        return self.length * self.width\r\n\r\n    def perimeter(self):\r\n        return 2 * (self.length + self.width)\r\n\r\nclass Circle(Shape):\r\n    def __init__(self, radius):\r\n        self.radius = radius\r\n\r\n    def area(self):\r\n        return 3.14 * self.radius**2\r\n\r\n    def perimeter(self):\r\n        return 2 * 3.14 * self.radius\r\n\r\n# Creating objects of concrete classes\r\nrectangle = Rectangle(5, 3)\r\ncircle = Circle(4)\r\n\r\n# Calling methods on the objects\r\nprint(\"Rectangle area:\", rectangle.area())\r\nprint(\"Rectangle perimeter:\", rectangle.perimeter())\r\n\r\nprint(\"Circle area:\", circle.area())\r\nprint(\"Circle perimeter:\", circle.perimeter())\r\n<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Code_explanation_of_using_abstraction_in_Python\"><\/span>Code explanation of using abstraction in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the above code example, we have a <a href=\"https:\/\/tutorpython.com\/tutorial\/abstraction-in-python\">Python abstract class<\/a> <code>Shape<\/code> that defines two abstract methods:<\/p>\n<p><code>area()<\/code> and <code>perimeter()<\/code><\/p>\n<p>Any class that inherits from <code>Shape<\/code> is required to implement these methods.<\/p>\n<p>The <code>Shape<\/code> class acts as a blueprint for different shapes. We then have two concrete classes, <code>Rectangle<\/code> and <code>Circle<\/code>, which inherit from <code>Shape<\/code> and provide their own implementations for the abstract methods. Each class calculates the area and perimeter based on its specific shape.<\/p>\n<p>By using abstraction, we can treat the <code>Rectangle<\/code> and <code>Circle<\/code> objects as instances of the abstract class <code>Shape<\/code>. This allows us to write generic code that works with any shape without worrying about the specific details of each shape&#8217;s implementation.<\/p>\n<p><strong>Note:<\/strong> the <code>abc<\/code> module is imported to make <code>Shape<\/code> an abstract base class (ABC). The <code>abstractmethod<\/code> <a href=\"https:\/\/tutorpython.com\/tutorial\/decorators-in-python\" target=\"_blank\" rel=\"noopener\">decorator<\/a> is used to mark the abstract methods, indicating that they must be overridden by subclasses. If a subclass does not implement these methods, a <a href=\"https:\/\/tutorpython.com\/tutorial\/exceptions-in-python\" target=\"_blank\" rel=\"noopener\"><code>TypeError<\/code><\/a> will be raised.<\/p>\n<p>Python Abstraction helps in designing modular and extensible code by providing a clear separation between the interface and the implementation. It allows developers to focus on high-level concepts and promotes code reuse and maintainability.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abstraction in Python is a fundamental concept that allows developers to create&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-8495","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"acf":[],"_links":{"self":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/comments?post=8495"}],"version-history":[{"count":10,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8495\/revisions"}],"predecessor-version":[{"id":8862,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8495\/revisions\/8862"}],"wp:attachment":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/media?parent=8495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/categories?post=8495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/tags?post=8495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}