{"id":2793,"date":"2021-11-09T16:16:50","date_gmt":"2021-11-09T16:16:50","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2793"},"modified":"2025-03-28T03:09:16","modified_gmt":"2025-03-28T03:09:16","slug":"python-interface-segregation-principle","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-interface-segregation-principle\/","title":{"rendered":"Python Interface Segregation Principle"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the interface segregation principle and how to apply it in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-interface-segregation-principle'>Introduction to the interface segregation principle <a href=\"#introduction-to-the-interface-segregation-principle\" class=\"anchor\" id=\"introduction-to-the-interface-segregation-principle\" title=\"Anchor for Introduction to the interface segregation principle\">#<\/a><\/h2>\n\n\n\n<p>The interface segregation principle is one of five SOLID principles in object-oriented programming:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>S<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-single-responsibility-principle\/\">Single responsibility Principle<\/a><\/li><li><strong>O<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-open-closed-principle\/\">Open-closed Principle<\/a><\/li><li><strong>L<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-liskov-substitution-principle\/\">Liskov Substitution Principle<\/a><\/li><li><strong>I<\/strong>\u00a0&#8211; Interface Segregation Principle<\/li><li><strong>D<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-dependency-inversion-principle\/\">Dependency Inversion Principle<\/a><\/li><\/ul>\n\n\n\n<p>An interface is a description of behaviors that an object can do. For example, when you press the power button on the TV remote control, it turns the TV on, and you don&#8217;t need to care how.<\/p>\n\n\n\n<p>In object-oriented programming, an interface is a set of <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-methods\/\">methods<\/a> an object must-have. The purpose of interfaces is to allow clients to request the correct methods of an object via its interface. <\/p>\n\n\n\n<p>Python uses <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-abstract-class\/\">abstract classes<\/a> as interfaces because it follows the so-called duck typing principle. The duck typing principle states that &#8220;if it walks like a duck and quacks like a duck, it must be a duck.&#8221; In other words, the methods of a class determine what its objects will be, not the type of the class.<\/p>\n\n\n\n<p>The interface segregation principle states that an interface should be as small a possible in terms of cohesion. In other words, it should do ONE thing. <\/p>\n\n\n\n<p>It doesn&#8217;t mean that the interface should have one method. An interface can have multiple cohesive methods. <\/p>\n\n\n\n<p>For example, the <code>Database<\/code> interface can have the <code>connect()<\/code> and <code>disconnect()<\/code> methods because they must go together. If the <code>Database<\/code> interface doesn&#8217;t use both methods, it&#8217;ll be incomplete.<\/p>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Robert_C._Martin\" target=\"_blank\" rel=\"noreferrer noopener\">Uncle Bob<\/a>, who is the originator of the SOLID term, explains the interface segregation principle by advising that &#8220;Make fine-grained interfaces that are client-specific. Clients should not be forced to implement interfaces they do not use.\u201d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='interface-segregation-principle-example'>Interface segregation principle example <a href=\"#interface-segregation-principle-example\" class=\"anchor\" id=\"interface-segregation-principle-example\" title=\"Anchor for Interface segregation principle example\">#<\/a><\/h2>\n\n\n\n<p>Consider the following example:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/Python-Interface-Segregation-Principle.svg\" alt=\"\" class=\"wp-image-2856\"\/><\/figure><\/div>\n\n\n\n<p>First, define a <code>Vehicle<\/code> abstract class that has two abstract methods, <code>go()<\/code> and <code>fly()<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> abc <span class=\"hljs-keyword\">import<\/span> ABC, abstractmethod\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Vehicle<\/span><span class=\"hljs-params\">(ABC)<\/span>:<\/span>\n<span class=\"hljs-meta\">    @abstractmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">go<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">pass<\/span>\n\n<span class=\"hljs-meta\">    @abstractmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fly<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">pass<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define the <code>Aircraft<\/code> class that inherits from the <code>Vehicle<\/code> class and implement both <code>go()<\/code> and <code>fly()<\/code> methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Aircraft<\/span><span class=\"hljs-params\">(Vehicle)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">go<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">\"Taxiing\"<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fly<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">\"Flying\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, define the <code>Car<\/code> class that inherits from the <code>Vehicle<\/code> class. Since a car cannot fly, we raise an exception in the <code>fly()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Car<\/span><span class=\"hljs-params\">(Vehicle)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">go<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">\"Going\"<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fly<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">raise<\/span> Exception(<span class=\"hljs-string\">'The car cannot fly'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this design the <code>Car<\/code> class must implement the <code>fly()<\/code> method from the <code>Vehicle<\/code> class that the <code>Car<\/code> class doesn&#8217;t use. Therefore, this design violates the interface segregation principle.<\/p>\n\n\n\n<p>To fix this, you need to split the <code>Vehicle<\/code> class into small ones and inherits from these classes from the <code>Aircraft<\/code> and <code>Car<\/code> classes:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/Python-Interface-Segregation-Principle-example.svg\" alt=\"\" class=\"wp-image-2857\"\/><\/figure><\/div>\n\n\n\n<p>First, split the <code>Vehicle<\/code> interface into two smaller interfaces: <code>Movable<\/code> and <code>Flyable<\/code>, and inherits the <code>Movable<\/code> class from the <code>Flyable<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Movable<\/span><span class=\"hljs-params\">(ABC)<\/span>:<\/span>\n<span class=\"hljs-meta\">    @abstractmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">go<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">pass<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Flyable<\/span><span class=\"hljs-params\">(Movable)<\/span>:<\/span>\n<span class=\"hljs-meta\">    @abstractmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fly<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">pass<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, inherits from the <code>Flyable<\/code> class from the <code>Aircraft<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Aircraft<\/span><span class=\"hljs-params\">(Flyable)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">go<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">\"Taxiing\"<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fly<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">\"Flying\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, inherit the <code>Movable<\/code> class from the <code>Car<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Car<\/span><span class=\"hljs-params\">(Movable)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">go<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">\"Going\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this design, the <code>Car<\/code> only need to implement the <code>go()<\/code> method that it needs. It doesn&#8217;t need to implement the <code>fly()<\/code> method that it doesn&#8217;t use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>The interface segregation principle advises that the interfaces should be small in terms of cohesions.<\/li><li>Make fine grained interfaces that are client-specific. Clients should not be forced to implement interfaces they do not use.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2793\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-interface-segregation-principle\/\"\n\t\t\t\tdata-post-title=\"Python Interface Segregation Principle\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2793\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-interface-segregation-principle\/\"\n\t\t\t\tdata-post-title=\"Python Interface Segregation Principle\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the interface segregation principle and how to apply it in Python.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":34,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2793","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2793"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2793\/revisions"}],"predecessor-version":[{"id":7174,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2793\/revisions\/7174"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}