{"id":2804,"date":"2021-11-10T02:47:16","date_gmt":"2021-11-10T02:47:16","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2804"},"modified":"2025-03-28T03:10:13","modified_gmt":"2025-03-28T03:10:13","slug":"python-dependency-inversion-principle","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-dependency-inversion-principle\/","title":{"rendered":"Python Dependency Inversion Principle"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python dependency inversion principle to make your code hi<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-dependency-inversion-principle'>Introduction to the dependency inversion principle <a href=\"#introduction-to-the-dependency-inversion-principle\" class=\"anchor\" id=\"introduction-to-the-dependency-inversion-principle\" title=\"Anchor for Introduction to the dependency inversion principle\">#<\/a><\/h2>\n\n\n\n<p>The dependency inversion principle is one of the five SOLID principles in object-oriented programming:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>S<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-single-responsibility-principle\/\">Single responsibility Principle<\/a><\/li>\n\n\n\n<li><strong>O<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-open-closed-principle\/\">Open-closed Principle<\/a><\/li>\n\n\n\n<li><strong>L<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-liskov-substitution-principle\/\">Liskov Substitution Principle<\/a><\/li>\n\n\n\n<li><strong>I<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-interface-segregation-principle\/\">Interface Segregation Principle<\/a><\/li>\n\n\n\n<li><strong>D<\/strong>\u00a0&#8211; Dependency Inversion Principle<\/li>\n<\/ul>\n\n\n\n<p>The dependency inversion principle states that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>High-level modules should not depend on low-level modules. Both should depend on abstractions.<\/li>\n\n\n\n<li>Abstractions should not depend on details. Details should depend on abstractions.<\/li>\n<\/ul>\n\n\n\n<p>The dependency inversion principle aims to reduce the coupling between classes by creating an abstraction layer between them.<\/p>\n\n\n\n<p>See the following example:<\/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-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">FXConverter<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, from_currency, to_currency, amount)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{amount}<\/span> <span class=\"hljs-subst\">{from_currency}<\/span> = <span class=\"hljs-subst\">{amount * <span class=\"hljs-number\">1.2<\/span>}<\/span> <span class=\"hljs-subst\">{to_currency}<\/span>'<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> amount * <span class=\"hljs-number\">1.2<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">start<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        converter = FXConverter()\n        converter.convert(<span class=\"hljs-string\">'EUR'<\/span>, <span class=\"hljs-string\">'USD'<\/span>, <span class=\"hljs-number\">100<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = App()\n    app.start()<\/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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgRlhDb252ZXJ0ZXI6CiAgICBkZWYgY29udmVydChzZWxmLCBmcm9tX2N1cnJlbmN5LCB0b19jdXJyZW5jeSwgYW1vdW50KToKICAgICAgICBwcmludChmJ3thbW91bnR9IHtmcm9tX2N1cnJlbmN5fSA9IHthbW91bnQgKiAxLjJ9IHt0b19jdXJyZW5jeX0nKQogICAgICAgIHJldHVybiBhbW91bnQgKiAxLjIKCgpjbGFzcyBBcHA6CiAgICBkZWYgc3RhcnQoc2VsZik6CiAgICAgICAgY29udmVydGVyID0gRlhDb252ZXJ0ZXIoKQogICAgICAgIGNvbnZlcnRlci5jb252ZXJ0KCdFVVInLCAnVVNEJywgMTAwKQoKCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICBhcHAgPSBBcHAoKQogICAgYXBwLnN0YXJ0KCk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">100 EUR = 120.0 USD<\/code><\/span><\/pre>\n\n\n<p>In this example, we have two classes <code>FXConverter<\/code> and <code>App<\/code>.<\/p>\n\n\n\n<p>The <code>FXConverter<\/code> class uses an API from an imaginary FX third-party to convert an amount from one currency to another. For simplicity, we hardcoded the exchange rate as <code>1.2<\/code>. In practice, you will need to make an API call to get the exchange rate.<\/p>\n\n\n\n<p>The <code>App<\/code> class has a <code>start()<\/code> method that uses an instance of the <code>FXconverter<\/code> class to convert 100 EUR to USD.<\/p>\n\n\n\n<p>The <code>App<\/code> is a high-level module. However, The <code>App<\/code> depends heavily on the <code>FXConverter<\/code> class that is dependent on the FX&#8217;s API.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/python-dependency-inversion-example.svg\" alt=\"\" class=\"wp-image-2846\"\/><\/figure>\n<\/div>\n\n\n<p>In the future, if the FX&#8217;s API changes, it&#8217;ll break the code. Also, if you want to use a different API, you&#8217;ll need to change the <code>App<\/code> class.<\/p>\n\n\n\n<p>To prevent this, you need to <strong>invert the dependency<\/strong> so that the <code>FXConverter<\/code> class needs to adapt to the <code>App<\/code> class.<\/p>\n\n\n\n<p>To do that, you define an interface and make the <code>App<\/code> dependent on it instead of <code>FXConverter<\/code> class. And then you change the <code>FXConverter<\/code> to comply with the interface.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/python-dependency-inversion-inheritance.svg\" alt=\"\" class=\"wp-image-2847\"\/><\/figure>\n<\/div>\n\n\n<p>First, define an <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-abstract-class\/\">abstract class<\/a> <code>CurrencyConverter<\/code> that acts as an interface. The <code>CurrencyConverter<\/code> class has the <code>convert()<\/code> method that all of its subclasses must implement:<\/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-keyword\">from<\/span> abc <span class=\"hljs-keyword\">import<\/span> ABC\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CurrencyConverter<\/span><span class=\"hljs-params\">(ABC)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, from_currency, to_currency, amount)<\/span> -&gt; float:<\/span>\n        <span class=\"hljs-keyword\">pass<\/span>\n<\/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>Second, redefine the <code>FXConverter<\/code> class so that it <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-inheritance\/\">inherits<\/a> from the <code>CurrencyConverter<\/code> class and implement the <code>convert()<\/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\">FXConverter<\/span><span class=\"hljs-params\">(CurrencyConverter)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, from_currency, to_currency, amount)<\/span> -&gt; float:<\/span>\n        print(<span class=\"hljs-string\">'Converting currency using FX API'<\/span>)\n        print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{amount}<\/span> <span class=\"hljs-subst\">{from_currency}<\/span> = <span class=\"hljs-subst\">{amount * <span class=\"hljs-number\">1.2<\/span>}<\/span> <span class=\"hljs-subst\">{to_currency}<\/span>'<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> amount * <span class=\"hljs-number\">2<\/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>Third, add the <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__init__\/\">__init__<\/a><\/code> method to the <code>App<\/code> class and initialize the <code>CurrencyConverter<\/code>&#8216;s object:<\/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\">App<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, converter: CurrencyConverter)<\/span>:<\/span>\n        self.converter = converter\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">start<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.converter.convert(<span class=\"hljs-string\">'EUR'<\/span>, <span class=\"hljs-string\">'USD'<\/span>, <span class=\"hljs-number\">100<\/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>Now, the <code>App<\/code> class depends on the <code>CurrencyConverter<\/code> interface, not the <code>FXConverter<\/code> class.<\/p>\n\n\n\n<p>The following creates an instance of the <code>FXConverter<\/code> and pass it to the <code>App<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    converter = FXConverter()\n    app = App(converter)\n    app.start()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Converting currency using FX API\n100 EUR = 120.0 USD<\/code><\/span><\/pre>\n\n\n<p>In the future, you can support another currency converter API by subclassing the <code>CurrencyConverter<\/code> class. For example, the following defines the <code>AlphaConverter<\/code> class that inherits from the <code>CurrencyConverter<\/code>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/python-dependency-inversion.svg\" alt=\"\" class=\"wp-image-2845\"\/><\/figure>\n<\/div>\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\">AlphaConverter<\/span><span class=\"hljs-params\">(CurrencyConverter)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, from_currency, to_currency, amount)<\/span> -&gt; float:<\/span>\n        print(<span class=\"hljs-string\">'Converting currency using Alpha API'<\/span>)\n        print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{amount}<\/span> <span class=\"hljs-subst\">{from_currency}<\/span> = <span class=\"hljs-subst\">{amount * <span class=\"hljs-number\">1.2<\/span>}<\/span> <span class=\"hljs-subst\">{to_currency}<\/span>'<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> amount * <span class=\"hljs-number\">1.15<\/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>Since the <code>AlphaConvert<\/code> class inherits from the <code>CurrencyConverter<\/code> class, you can use its object in the <code>App<\/code> class without changing the <code>App<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    converter = AlphaConverter()\n    app = App(converter)\n    app.start()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Converting currency using Alpha API\n100 EUR = 120.0 USD<\/code><\/span><\/pre>\n\n\n<p>Put it all together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CurrencyConverter<\/span><span class=\"hljs-params\">(ABC)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, from_currency, to_currency, amount)<\/span> -&gt; float:<\/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\">FXConverter<\/span><span class=\"hljs-params\">(CurrencyConverter)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, from_currency, to_currency, amount)<\/span> -&gt; float:<\/span>\n        print(<span class=\"hljs-string\">'Converting currency using FX API'<\/span>)\n        print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{amount}<\/span> <span class=\"hljs-subst\">{from_currency}<\/span> = <span class=\"hljs-subst\">{amount * <span class=\"hljs-number\">1.2<\/span>}<\/span> <span class=\"hljs-subst\">{to_currency}<\/span>'<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> amount * <span class=\"hljs-number\">1.15<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">AlphaConverter<\/span><span class=\"hljs-params\">(CurrencyConverter)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, from_currency, to_currency, amount)<\/span> -&gt; float:<\/span>\n        print(<span class=\"hljs-string\">'Converting currency using Alpha API'<\/span>)\n        print(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{amount}<\/span> <span class=\"hljs-subst\">{from_currency}<\/span> = <span class=\"hljs-subst\">{amount * <span class=\"hljs-number\">1.2<\/span>}<\/span> <span class=\"hljs-subst\">{to_currency}<\/span>'<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> amount * <span class=\"hljs-number\">1.2<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, converter: CurrencyConverter)<\/span>:<\/span>\n        self.converter = converter\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">start<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.converter.convert(<span class=\"hljs-string\">'EUR'<\/span>, <span class=\"hljs-string\">'USD'<\/span>, <span class=\"hljs-number\">100<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    converter = AlphaConverter()\n    app = App(converter)\n    app.start()\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBhYmMgaW1wb3J0IEFCQwoKCmNsYXNzIEN1cnJlbmN5Q29udmVydGVyKEFCQyk6CiAgICBkZWYgY29udmVydChzZWxmLCBmcm9tX2N1cnJlbmN5LCB0b19jdXJyZW5jeSwgYW1vdW50KSAtPiBmbG9hdDoKICAgICAgICBwYXNzCgoKY2xhc3MgRlhDb252ZXJ0ZXIoQ3VycmVuY3lDb252ZXJ0ZXIpOgogICAgZGVmIGNvbnZlcnQoc2VsZiwgZnJvbV9jdXJyZW5jeSwgdG9fY3VycmVuY3ksIGFtb3VudCkgLT4gZmxvYXQ6CiAgICAgICAgcHJpbnQoJ0NvbnZlcnRpbmcgY3VycmVuY3kgdXNpbmcgRlggQVBJJykKICAgICAgICBwcmludChmJ3thbW91bnR9IHtmcm9tX2N1cnJlbmN5fSA9IHthbW91bnQgKiAxLjJ9IHt0b19jdXJyZW5jeX0nKQogICAgICAgIHJldHVybiBhbW91bnQgKiAxLjE1CgoKY2xhc3MgQWxwaGFDb252ZXJ0ZXIoQ3VycmVuY3lDb252ZXJ0ZXIpOgogICAgZGVmIGNvbnZlcnQoc2VsZiwgZnJvbV9jdXJyZW5jeSwgdG9fY3VycmVuY3ksIGFtb3VudCkgLT4gZmxvYXQ6CiAgICAgICAgcHJpbnQoJ0NvbnZlcnRpbmcgY3VycmVuY3kgdXNpbmcgQWxwaGEgQVBJJykKICAgICAgICBwcmludChmJ3thbW91bnR9IHtmcm9tX2N1cnJlbmN5fSA9IHthbW91bnQgKiAxLjJ9IHt0b19jdXJyZW5jeX0nKQogICAgICAgIHJldHVybiBhbW91bnQgKiAxLjIKCgpjbGFzcyBBcHA6CiAgICBkZWYgX19pbml0X18oc2VsZiwgY29udmVydGVyOiBDdXJyZW5jeUNvbnZlcnRlcik6CiAgICAgICAgc2VsZi5jb252ZXJ0ZXIgPSBjb252ZXJ0ZXIKCiAgICBkZWYgc3RhcnQoc2VsZik6CiAgICAgICAgc2VsZi5jb252ZXJ0ZXIuY29udmVydCgnRVVSJywgJ1VTRCcsIDEwMCkKCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgY29udmVydGVyID0gQWxwaGFDb252ZXJ0ZXIoKQogICAgYXBwID0gQXBwKGNvbnZlcnRlcikKICAgIGFwcC5zdGFydCgp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Converting currency using Alpha API\n100 EUR = 120.0 USD<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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\">\n<li>Use the dependency inversion principle to make your code more robust by making the high-level module dependent on the abstraction, not the concrete implementation.<\/li>\n<\/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=\"2804\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-dependency-inversion-principle\/\"\n\t\t\t\tdata-post-title=\"Python Dependency Inversion 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=\"2804\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-dependency-inversion-principle\/\"\n\t\t\t\tdata-post-title=\"Python Dependency Inversion 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>Summary: in this tutorial, you&#8217;ll learn about the Python dependency inversion principle to make your code hi Introduction to the dependency inversion principle # The dependency inversion principle is one of the five SOLID principles in object-oriented programming: The dependency inversion principle states that: The dependency inversion principle aims to reduce the coupling between classes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":35,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2804","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2804","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=2804"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2804\/revisions"}],"predecessor-version":[{"id":7175,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2804\/revisions\/7175"}],"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=2804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}