{"id":2409,"date":"2023-01-31T17:08:36","date_gmt":"2023-01-31T17:08:36","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2409"},"modified":"2023-01-31T17:08:37","modified_gmt":"2023-01-31T17:08:37","slug":"python-interface","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/01\/31\/python-interface\/","title":{"rendered":"Python: Interface"},"content":{"rendered":"\n<p>n Python, unlike some other programming languages, there is no explicit syntax for defining interfaces. However, you can create a similar concept by defining an abstract base class (ABC) that contains abstract methods. An abstract method is a method that is declared in a base class, but has no implementation. Subclasses of the abstract base class must implement these abstract methods.<\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-cyan-bluish-gray-background-color has-text-color has-background\"><code>from abc import ABC, abstractmethod\n\nclass Shape(ABC):\n    @abstractmethod\n    def area(self):\n        pass\n\nclass Square(Shape):\n    def __init__(self, side):\n        self.side = side\n\n    def area(self):\n        return self.side * self.side\n\nsquare = Square(5)\nprint(square.area())  # 25\n<\/code><\/pre>\n\n\n\n<p>In the example above, the <code>Shape<\/code> class is defined as an abstract base class using the <code>ABC<\/code> class from the <code>abc<\/code> module. <\/p>\n\n\n\n<p>The <code>area<\/code> method is defined as an abstract method using the <code>@abstractmethod<\/code> decorator. The <code>Square<\/code> class is a concrete implementation of the <code>Shape<\/code> interface, and implements the <code>area<\/code> method. When you create an instance of the <code>Square<\/code> class and call its <code>area<\/code> method, the implementation of the <code>area<\/code> method in the <code>Square<\/code> class is executed, and the result is returned.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-black-color has-pale-pink-background-color has-text-color has-background has-medium-font-size\" style=\"border-width:7px;border-radius:75px;font-style:normal;font-weight:600\"><blockquote><p>The &#8220;@&#8221; symbol in Python is used to define decorators. Decorators are a way to modify the behavior of a function, class, or method by wrapping it in another function.<\/p><cite>Help<\/cite><\/blockquote><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>n Python, unlike some other programming languages, there is no explicit syntax for defining interfaces. However, you can create a similar concept by defining an abstract base class (ABC) that contains abstract methods. An abstract method is a method that is declared in a base class, but has no implementation. Subclasses of the abstract base [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[43,270],"class_list":["post-2409","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-interface","tag-python","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2409"}],"version-history":[{"count":1,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2409\/revisions"}],"predecessor-version":[{"id":2411,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2409\/revisions\/2411"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2399"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}