{"id":2772,"date":"2021-11-08T09:38:59","date_gmt":"2021-11-08T09:38:59","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2772"},"modified":"2025-03-28T03:08:39","modified_gmt":"2025-03-28T03:08:39","slug":"python-liskov-substitution-principle","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-liskov-substitution-principle\/","title":{"rendered":"Python Liskov Substitution Principle"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Liskov Substitution Principle and how to implement it in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-liskov-substitution-principle'>Introduction to the Liskov substitution principle <a href=\"#introduction-to-the-liskov-substitution-principle\" class=\"anchor\" id=\"introduction-to-the-liskov-substitution-principle\" title=\"Anchor for Introduction to the Liskov substitution principle\">#<\/a><\/h2>\n\n\n\n<p>The Liskov substitution principle (LSV) is one of the five principles in the SOLID principles. The L in SOLID stands for the Liskov substitution principle.<\/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; Liskov Substitution Principle<\/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; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-dependency-inversion-principle\/\">Dependency Inversion Principle<\/a><\/li>\n<\/ul>\n\n\n\n<p>The Liskov substitution principle states that a child class must be substitutable for its parent class. Liskov substitution principle aims to ensure that the child class can assume the place of its parent class without causing any errors.<\/p>\n\n\n\n<p>Consider 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-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\">Notification<\/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\">notify<\/span><span class=\"hljs-params\">(self, message, email)<\/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\">Email<\/span><span class=\"hljs-params\">(Notification)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">notify<\/span><span class=\"hljs-params\">(self, message, email)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Send <span class=\"hljs-subst\">{message}<\/span> to <span class=\"hljs-subst\">{email}<\/span>'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SMS<\/span><span class=\"hljs-params\">(Notification)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">notify<\/span><span class=\"hljs-params\">(self, message, phone)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Send <span class=\"hljs-subst\">{message}<\/span> to <span class=\"hljs-subst\">{phone}<\/span>'<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    notification = SMS()\n    notification.notify(<span class=\"hljs-string\">'Hello'<\/span>, <span class=\"hljs-string\">'john@test.com'<\/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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBhYmMgaW1wb3J0IEFCQywgYWJzdHJhY3RtZXRob2QKCgpjbGFzcyBOb3RpZmljYXRpb24oQUJDKToKICAgIEBhYnN0cmFjdG1ldGhvZAogICAgZGVmIG5vdGlmeShzZWxmLCBtZXNzYWdlLCBlbWFpbCk6CiAgICAgICAgcGFzcwoKCmNsYXNzIEVtYWlsKE5vdGlmaWNhdGlvbik6CiAgICBkZWYgbm90aWZ5KHNlbGYsIG1lc3NhZ2UsIGVtYWlsKToKICAgICAgICBwcmludChmJ1NlbmQge21lc3NhZ2V9IHRvIHtlbWFpbH0nKQoKCmNsYXNzIFNNUyhOb3RpZmljYXRpb24pOgogICAgZGVmIG5vdGlmeShzZWxmLCBtZXNzYWdlLCBwaG9uZSk6CiAgICAgICAgcHJpbnQoZidTZW5kIHttZXNzYWdlfSB0byB7cGhvbmV9JykKCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgbm90aWZpY2F0aW9uID0gU01TKCkKICAgIG5vdGlmaWNhdGlvbi5ub3RpZnkoJ0hlbGxvJywgJ2pvaG5AdGVzdC5jb20nKQ%3D%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\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Send Hello to john@test.com<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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<p>In this example, we have three classes: <code>Notification<\/code>, <code>Email<\/code>, and <code>SMS<\/code>. The <code>Email<\/code> and <code>SMS<\/code> classes inherit from the <code>Notification<\/code> class.<\/p>\n\n\n\n<p>The <code>Notification<\/code> <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-abstract-class\/\">abstract class<\/a> has <code>notify()<\/code> method that sends a message to an email address.<\/p>\n\n\n\n<p>The <code>notify()<\/code> method of the <code>Email<\/code> class sends a message to an email, which is fine.<\/p>\n\n\n\n<p>However, the <code>SMS<\/code> class uses a phone number, not an email, for sending a message. Therefore, we need to change the signature of the <code>notify()<\/code> method of the <code>SMS<\/code> class to accept a phone number instead of an email.<\/p>\n\n\n\n<p>The following <code>NotificationManager<\/code> class uses the <code>Notification<\/code> object to send a message to a <code>Contact<\/code>:<\/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\">Contact<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, email, phone)<\/span>:<\/span>\n        self.name = name\n        self.email = email\n        self.phone = phone\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">NotificationManager<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, notification, contact)<\/span>:<\/span>\n        self.contact = contact\n        self.notification = notification\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">send<\/span><span class=\"hljs-params\">(self, message)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> isinstance(self.notification, Email):\n            self.notification.notify(message, contact.email)\n        <span class=\"hljs-keyword\">elif<\/span> isinstance(self.notification, SMS):\n            self.notification.notify(message, contact.phone)\n        <span class=\"hljs-keyword\">else<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> Exception(<span class=\"hljs-string\">'The notification is not supported'<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    contact = Contact(<span class=\"hljs-string\">'John Doe'<\/span>, <span class=\"hljs-string\">'john@test.com'<\/span>, <span class=\"hljs-string\">'(408)-888-9999'<\/span>)\n    notification_manager = NotificationManager(SMS(), contact)\n    notification_manager.send(<span class=\"hljs-string\">'Hello John'<\/span>)\n<\/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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgQ29udGFjdDoKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBuYW1lLCBlbWFpbCwgcGhvbmUpOgogICAgICAgIHNlbGYubmFtZSA9IG5hbWUKICAgICAgICBzZWxmLmVtYWlsID0gZW1haWwKICAgICAgICBzZWxmLnBob25lID0gcGhvbmUKCgpjbGFzcyBOb3RpZmljYXRpb25NYW5hZ2VyOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIG5vdGlmaWNhdGlvbiwgY29udGFjdCk6CiAgICAgICAgc2VsZi5jb250YWN0ID0gY29udGFjdAogICAgICAgIHNlbGYubm90aWZpY2F0aW9uID0gbm90aWZpY2F0aW9uCgogICAgZGVmIHNlbmQoc2VsZiwgbWVzc2FnZSk6CiAgICAgICAgaWYgaXNpbnN0YW5jZShzZWxmLm5vdGlmaWNhdGlvbiwgRW1haWwpOgogICAgICAgICAgICBzZWxmLm5vdGlmaWNhdGlvbi5ub3RpZnkobWVzc2FnZSwgY29udGFjdC5lbWFpbCkKICAgICAgICBlbGlmIGlzaW5zdGFuY2Uoc2VsZi5ub3RpZmljYXRpb24sIFNNUyk6CiAgICAgICAgICAgIHNlbGYubm90aWZpY2F0aW9uLm5vdGlmeShtZXNzYWdlLCBjb250YWN0LnBob25lKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHJhaXNlIEV4Y2VwdGlvbignVGhlIG5vdGlmaWNhdGlvbiBpcyBub3Qgc3VwcG9ydGVkJykKCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgY29udGFjdCA9IENvbnRhY3QoJ0pvaG4gRG9lJywgJ2pvaG5AdGVzdC5jb20nLCAnKDQwOCktODg4LTk5OTknKQogICAgbm90aWZpY2F0aW9uX21hbmFnZXIgPSBOb3RpZmljYXRpb25NYW5hZ2VyKFNNUygpLCBjb250YWN0KQogICAgbm90aWZpY2F0aW9uX21hbmFnZXIuc2VuZCgnSGVsbG8gSm9obicp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The <code>send()<\/code> method of the <code>NoticationManager<\/code> class accepts a notification object. It checks whether the notification is an instance of the <code>Email<\/code> or <code>SMS<\/code> and passes the email and phone of contact to the <code>notify()<\/code> method respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='conform-with-the-liskov-substitution-principle'>Conform with the Liskov substitution principle <a href=\"#conform-with-the-liskov-substitution-principle\" class=\"anchor\" id=\"conform-with-the-liskov-substitution-principle\" title=\"Anchor for Conform with the Liskov substitution principle\">#<\/a><\/h2>\n\n\n\n<p>First, redefine the <code>notify()<\/code> method of the <code>Notification<\/code> class so that it doesn&#8217;t include the <code>email<\/code> parameter:<\/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\">Notification<\/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\">notify<\/span><span class=\"hljs-params\">(self, message)<\/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, add the <code>email<\/code> parameter to the <code>__init__<\/code> method of the <code>Email<\/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\">Email<\/span><span class=\"hljs-params\">(Notification)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, email)<\/span>:<\/span>\n        self.email = email\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">notify<\/span><span class=\"hljs-params\">(self, message)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Send \"<span class=\"hljs-subst\">{message}<\/span>\" to <span class=\"hljs-subst\">{self.email}<\/span>'<\/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, add the <code>phone<\/code> parameter to the <code>__init__<\/code> method of the <code>SMS<\/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\">SMS<\/span><span class=\"hljs-params\">(Notification)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, phone)<\/span>:<\/span>\n        self.phone = phone\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">notify<\/span><span class=\"hljs-params\">(self, message)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Send \"<span class=\"hljs-subst\">{message}<\/span>\" to <span class=\"hljs-subst\">{self.phone}<\/span>'<\/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>Fourth, change the <code>NotificationManager<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" 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\">NotificationManager<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, notification)<\/span>:<\/span>\n        self.notification = notification\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">send<\/span><span class=\"hljs-params\">(self, message)<\/span>:<\/span>\n        self.notification.notify(message)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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>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, abstractmethod\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Notification<\/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\">notify<\/span><span class=\"hljs-params\">(self, message)<\/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\">Email<\/span><span class=\"hljs-params\">(Notification)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, email)<\/span>:<\/span>\n        self.email = email\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">notify<\/span><span class=\"hljs-params\">(self, message)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Send \"<span class=\"hljs-subst\">{message}<\/span>\" to <span class=\"hljs-subst\">{self.email}<\/span>'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SMS<\/span><span class=\"hljs-params\">(Notification)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, phone)<\/span>:<\/span>\n        self.phone = phone\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">notify<\/span><span class=\"hljs-params\">(self, message)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Send \"<span class=\"hljs-subst\">{message}<\/span>\" to <span class=\"hljs-subst\">{self.phone}<\/span>'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Contact<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, email, phone)<\/span>:<\/span>\n        self.name = name\n        self.email = email\n        self.phone = phone\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">NotificationManager<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, notification)<\/span>:<\/span>\n        self.notification = notification\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">send<\/span><span class=\"hljs-params\">(self, message)<\/span>:<\/span>\n        self.notification.notify(message)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    contact = Contact(<span class=\"hljs-string\">'John Doe'<\/span>, <span class=\"hljs-string\">'john@test.com'<\/span>, <span class=\"hljs-string\">'(408)-888-9999'<\/span>)\n\n    sms_notification = SMS(contact.phone)\n    email_notification = Email(contact.email)\n\n    notification_manager = NotificationManager(sms_notification)\n    notification_manager.send(<span class=\"hljs-string\">'Hello John'<\/span>)\n\n    notification_manager.notification = email_notification\n    notification_manager.send(<span class=\"hljs-string\">'Hi John'<\/span>)\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=ZnJvbSBhYmMgaW1wb3J0IEFCQywgYWJzdHJhY3RtZXRob2QKCgpjbGFzcyBOb3RpZmljYXRpb24oQUJDKToKICAgIEBhYnN0cmFjdG1ldGhvZAogICAgZGVmIG5vdGlmeShzZWxmLCBtZXNzYWdlKToKICAgICAgICBwYXNzCgoKY2xhc3MgRW1haWwoTm90aWZpY2F0aW9uKToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBlbWFpbCk6CiAgICAgICAgc2VsZi5lbWFpbCA9IGVtYWlsCgogICAgZGVmIG5vdGlmeShzZWxmLCBtZXNzYWdlKToKICAgICAgICBwcmludChmJ1NlbmQgInttZXNzYWdlfSIgdG8ge3NlbGYuZW1haWx9JykKCgpjbGFzcyBTTVMoTm90aWZpY2F0aW9uKToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBwaG9uZSk6CiAgICAgICAgc2VsZi5waG9uZSA9IHBob25lCgogICAgZGVmIG5vdGlmeShzZWxmLCBtZXNzYWdlKToKICAgICAgICBwcmludChmJ1NlbmQgInttZXNzYWdlfSIgdG8ge3NlbGYucGhvbmV9JykKCgpjbGFzcyBDb250YWN0OgogICAgZGVmIF9faW5pdF9fKHNlbGYsIG5hbWUsIGVtYWlsLCBwaG9uZSk6CiAgICAgICAgc2VsZi5uYW1lID0gbmFtZQogICAgICAgIHNlbGYuZW1haWwgPSBlbWFpbAogICAgICAgIHNlbGYucGhvbmUgPSBwaG9uZQoKCmNsYXNzIE5vdGlmaWNhdGlvbk1hbmFnZXI6CiAgICBkZWYgX19pbml0X18oc2VsZiwgbm90aWZpY2F0aW9uKToKICAgICAgICBzZWxmLm5vdGlmaWNhdGlvbiA9IG5vdGlmaWNhdGlvbgoKICAgIGRlZiBzZW5kKHNlbGYsIG1lc3NhZ2UpOgogICAgICAgIHNlbGYubm90aWZpY2F0aW9uLm5vdGlmeShtZXNzYWdlKQoKCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICBjb250YWN0ID0gQ29udGFjdCgnSm9obiBEb2UnLCAnam9obkB0ZXN0LmNvbScsICcoNDA4KS04ODgtOTk5OScpCgogICAgc21zX25vdGlmaWNhdGlvbiA9IFNNUyhjb250YWN0LnBob25lKQogICAgZW1haWxfbm90aWZpY2F0aW9uID0gRW1haWwoY29udGFjdC5lbWFpbCkKCiAgICBub3RpZmljYXRpb25fbWFuYWdlciA9IE5vdGlmaWNhdGlvbk1hbmFnZXIoc21zX25vdGlmaWNhdGlvbikKICAgIG5vdGlmaWNhdGlvbl9tYW5hZ2VyLnNlbmQoJ0hlbGxvIEpvaG4nKQoKICAgIG5vdGlmaWNhdGlvbl9tYW5hZ2VyLm5vdGlmaWNhdGlvbiA9IGVtYWlsX25vdGlmaWNhdGlvbgogICAgbm90aWZpY2F0aW9uX21hbmFnZXIuc2VuZCgnSGkgSm9obicp\" 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\">Send \"Hello John\" to (408)-888-9999\nSend \"Hi John\" to john@test.com<\/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>The Liskov substitution principle states that a child class must be substitutable for its parent class.<\/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=\"2772\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-liskov-substitution-principle\/\"\n\t\t\t\tdata-post-title=\"Python Liskov Substitution 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=\"2772\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-liskov-substitution-principle\/\"\n\t\t\t\tdata-post-title=\"Python Liskov Substitution 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 Liskov Substitution Principle and how to implement it in Python. Introduction to the Liskov substitution principle # The Liskov substitution principle (LSV) is one of the five principles in the SOLID principles. The L in SOLID stands for the Liskov substitution principle. The Liskov substitution principle states [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":33,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2772","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2772","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=2772"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2772\/revisions"}],"predecessor-version":[{"id":7173,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2772\/revisions\/7173"}],"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=2772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}