{"id":174,"date":"2024-01-25T14:47:55","date_gmt":"2024-01-25T14:47:55","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=174"},"modified":"2024-01-25T14:47:57","modified_gmt":"2024-01-25T14:47:57","slug":"python-overriding-method","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-overriding-method\/","title":{"rendered":"Python Overriding Method"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use Python overriding method to allow a child class to provide a specific implementation of a method that is provided by one of its parent classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python overridding method<\/h2>\n\n\n\n<p>The overriding method allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes.<\/p>\n\n\n\n<p>Let\u2019s take an example to understand the overriding method better.<\/p>\n\n\n\n<p>First, define the\u00a0<code>Employee<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Employee: def __init__(self, name, base_pay): self.name = name self.base_pay = base_pay def get_pay(self): return self.base_pay<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Employee<\/code>\u00a0class has two\u00a0instance variables\u00a0<code>name<\/code>\u00a0and\u00a0<code>base_pay<\/code>. It also has the\u00a0<code>get_pay()<\/code>\u00a0method that returns the\u00a0<code>base_pay<\/code>.<\/p>\n\n\n\n<p>Second, define the\u00a0<code>SalesEmployee<\/code>\u00a0that inherits from the\u00a0<code>Employee<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class SalesEmployee(Employee): def __init__(self, name, base_pay, sales_incentive): self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>SalesEmployee<\/code>&nbsp;class has three instance attributes:&nbsp;<code>name<\/code>,&nbsp;<code>base_pay<\/code>, and&nbsp;<code>sales_incentive<\/code>.<\/p>\n\n\n\n<p>Third, create a new instance of the\u00a0<code>SalesEmployee<\/code>\u00a0class and display the pay:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>john = SalesEmployee('John', 5000, 1500) print(john.get_pay())<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>5000<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>get_pay()<\/code>&nbsp;method returns only the&nbsp;<code>base_pay<\/code>, not the sum of the&nbsp;<code>base_pay<\/code>&nbsp;and&nbsp;<code>sales_incentive<\/code>.<\/p>\n\n\n\n<p>When you call the&nbsp;<code>get_pay()<\/code>&nbsp;from the instance of the&nbsp;<code>SalesEmployee<\/code>&nbsp;class, Python executes the&nbsp;<code>get_pay()<\/code>&nbsp;method of the&nbsp;<code>Employee<\/code>&nbsp;class, which returns the&nbsp;<code>base_pay<\/code>.<\/p>\n\n\n\n<p>To include the sales incentive in the pay, you need to redefine the\u00a0<code>get_pay()<\/code>\u00a0method in the\u00a0<code>SalesEmployee<\/code>\u00a0class as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class SalesEmployee(Employee): def __init__(self, name, base_pay, sales_incentive): self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive def get_pay(self): return self.base_pay + self.sales_incentive<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this case, we say that the&nbsp;<code>get_pay()<\/code>&nbsp;method in the&nbsp;<code>SalesEmployee<\/code>&nbsp;class overrides the&nbsp;<code>get_pay()<\/code>&nbsp;method in the&nbsp;<code>Employee<\/code>&nbsp;class.<\/p>\n\n\n\n<p>When you call the\u00a0<code>get_pay()<\/code>\u00a0method of the\u00a0<code>SalesEmployee<\/code>\u2018s object, Python will call the\u00a0<code>get_pay()<\/code>\u00a0method in the\u00a0<code>SalesEmployee<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>john = SalesEmployee('John', 5000, 1500) print(john.get_pay())<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>6500<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you create an instance of the\u00a0<code>Employee<\/code>\u00a0class, Python will call the\u00a0<code>get_pay()<\/code>\u00a0method of the\u00a0<code>Employee<\/code>\u00a0class, not the\u00a0<code>get_pay()<\/code>\u00a0method of the\u00a0<code>SalesEmployee<\/code>\u00a0class. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>jane = Employee('Jane', 5000) print(jane.get_pay())<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Put it all together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Employee: def __init__(self, name, base_pay): self.name = name self.base_pay = base_pay def get_pay(self): return self.base_pay class SalesEmployee(Employee): def __init__(self, name, base_pay, sales_incentive): self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive def get_pay(self): return self.base_pay + self.sales_incentive if __name__ == '__main__': john = SalesEmployee('John', 5000, 1500) print(john.get_pay()) jane = Employee('Jane', 5000) print(jane.get_pay())<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced method overriding example<\/h2>\n\n\n\n<p>The following defines the\u00a0<code>Parser<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Parser: def __init__(self, text): self.text = text def email(self): match = re.search(r'&#91;a-z0-9\\.\\-+_]+@&#91;a-z0-9\\.\\-+_]+\\.&#91;a-z]+', self.text) if match: return match.group(0) return None def phone(self): match = re.search(r'\\d{3}-\\d{3}-\\d{4}', self.text) if match: return match.group(0) return None def parse(self): return { 'email': self.email(), 'phone': self.phone() }<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>Parser<\/code>&nbsp;class has an attribute&nbsp;<code>text<\/code>&nbsp;which specifies a piece of text to be parsed. Also, the&nbsp;<code>Parser<\/code>&nbsp;class has three methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<code>email()<\/code>&nbsp;method parses a text and returns the email.<\/li>\n\n\n\n<li>The&nbsp;<code>phone()<\/code>&nbsp;method parses a text and returns a phone number in the format&nbsp;<code>nnn-nnnn-nnnn<\/code>&nbsp;where&nbsp;<code>n<\/code>&nbsp;is a number from 0 to 9 e.g.,&nbsp;<code>408-205-5663<\/code>.<\/li>\n\n\n\n<li>The&nbsp;<code>parse()<\/code>&nbsp;method returns a dictionary that contains two elements&nbsp;<code>email<\/code>&nbsp;and&nbsp;<code>phone<\/code>. It calls the&nbsp;<code>email()<\/code>&nbsp;and&nbsp;<code>phone()<\/code>&nbsp;method to extract the email and phone from the&nbsp;<code>text<\/code>&nbsp;attribute.<\/li>\n<\/ul>\n\n\n\n<p>The following uses the\u00a0<code>Parser<\/code>\u00a0class to extract email and phone:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s = 'Contact us via 408-205-5663 or email@test.com' parser = Parser(s) print(parser.parse())<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'email': 'email@test.com', 'phone': '408-205-5663'}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Suppose you need to extract phone numbers in the format&nbsp;<code>n-nnn-nnn-nnnn<\/code>, which is the UK phone number format. Also, you want to use extract email like the&nbsp;<code>Parser<\/code>&nbsp;class<\/p>\n\n\n\n<p>To do it, you can define a new class called\u00a0<code>UkParser<\/code>\u00a0that inherits from the\u00a0<code>Parser<\/code>\u00a0class. In the\u00a0<code>UkParser<\/code>\u00a0class, you override the\u00a0<code>phone()<\/code>\u00a0method as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class UkParser(Parser): def phone(self): match = re.search(r'(\\+\\d{1}-\\d{3}-\\d{3}-\\d{4})', self.text) if match: return match.group(0) return None<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following use the\u00a0<code>UkParser<\/code>\u00a0class to extract a phone number (in UK format) and email from a text:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s2 = 'Contact me via +1-650-453-3456 or email@test.co.uk' parser = UkParser(s2) print(parser.parse())<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'email': 'email@test.co.uk', 'phone': '+1-650-453-3456'}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>parser<\/code>&nbsp;calls the&nbsp;<code>parse()<\/code>&nbsp;method from the parent class which is the Parser class. In turn, the&nbsp;<code>parse()<\/code>&nbsp;method calls the&nbsp;<code>email()<\/code>&nbsp;and&nbsp;<code>phone()<\/code>&nbsp;methods.<\/p>\n\n\n\n<p>However, the\u00a0<code>parser()<\/code>\u00a0doesn\u2019t call the\u00a0<code>phone()<\/code>\u00a0method of the\u00a0<code>Parser<\/code>\u00a0class but the\u00a0<code>phone()<\/code>\u00a0method of the\u00a0<code>UkParser<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>parser.parse()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The reason is that inside the&nbsp;<code>parse()<\/code>&nbsp;method, the&nbsp;<code>self<\/code>&nbsp;is the&nbsp;<code>parser<\/code>&nbsp;which is an instance of the&nbsp;<code>UkParser<\/code>&nbsp;class.<\/p>\n\n\n\n<p>Therefore, when you call&nbsp;<code>self.phone()<\/code>&nbsp;method inside the&nbsp;<code>parse()<\/code>&nbsp;method, Python will look for the&nbsp;<code>phone()<\/code>&nbsp;method that is bound to the instance of the&nbsp;<code>UkParser<\/code>.<\/p>\n\n\n\n<p>Put it all together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import re class Parser: def __init__(self, text): self.text = text def email(self): match = re.search(r'&#91;a-z0-9\\.\\-+_]+@&#91;a-z0-9\\.\\-+_]+\\.&#91;a-z]+', self.text) if match: return match.group(0) return None def phone(self): match = re.search(r'\\d{3}-\\d{3}-\\d{4}', self.text) if match: return match.group(0) return None def parse(self): return { 'email': self.email(), 'phone': self.phone() } class UkParser(Parser): def phone(self): match = re.search(r'(\\+\\d{1}-\\d{3}-\\d{3}-\\d{4})', self.text) if match: return match.group(0) return None if __name__ == '__main__': s = 'Contact us via 408-205-5663 or email@test.com' parser = Parser(s) print(parser.parse()) s2 = 'Contact me via +1-650-453-3456 or email@test.co.uk' parser = UkParser(s2) print(parser.parse())<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Overriding attributes<\/h2>\n\n\n\n<p>The following shows how to implement the Parser and UkParser classes by overriding attributes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import re class Parser: phone_pattern = r'\\d{3}-\\d{3}-\\d{4}' def __init__(self, text): self.text = text def email(self): match = re.search(r'&#91;a-z0-9\\.\\-+_]+@&#91;a-z0-9\\.\\-+_]+\\.&#91;a-z]+', self.text) if match: return match.group(0) return None def phone(self): match = re.search(self.phone_pattern, self.text) if match: return match.group(0) return None def parse(self): return { 'email': self.email(), 'phone': self.phone() } class UkParser(Parser): phone_pattern = r'(\\+\\d{1}-\\d{3}-\\d{3}-\\d{4})' if __name__ == '__main__': s = 'Contact us via 408-205-5663 or email@test.com' parser = Parser(s) print(parser.parse()) s2 = 'Contact me via +1-650-453-3456 or email@test.co.uk' parser = UkParser(s2) print(parser.parse())<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>Parser<\/code>&nbsp;has a class variable&nbsp;<code>phone_pattern<\/code>. The&nbsp;<code>phone()<\/code>&nbsp;method in the&nbsp;<code>Parser<\/code>&nbsp;class uses the&nbsp;<code>phone_pattern<\/code>&nbsp;to extract a phone number.<\/p>\n\n\n\n<p>The&nbsp;<code>UkParser<\/code>&nbsp;child class redefines (or overrides) the&nbsp;<code>phone_pattern<\/code>&nbsp;class attribute.<\/p>\n\n\n\n<p>If you call the&nbsp;<code>parse()<\/code>&nbsp;method from the&nbsp;<code>UkParser<\/code>\u2018s instance, the&nbsp;<code>parse()<\/code>&nbsp;method calls the&nbsp;<code>phone()<\/code>&nbsp;method that uses the&nbsp;<code>phone_pattern<\/code>&nbsp;defined in the&nbsp;<code>UkParser<\/code>&nbsp;class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use Python overriding method to allow a child class to provide a specific implementation of a method that is provided by one of its parent classes. Introduction to Python overridding method The overriding method allows a child class to provide a specific implementation of a method that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-4-single-inheritance"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/174\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}