{"id":33515,"date":"2020-03-17T13:52:39","date_gmt":"2020-03-17T08:22:39","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=33515"},"modified":"2020-04-24T19:38:06","modified_gmt":"2020-04-24T14:08:06","slug":"operator-overloading-in-python","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/operator-overloading-in-python\/","title":{"rendered":"Operator Overloading in Python"},"content":{"rendered":"<p>Operator Overloading means giving extended meaning beyond their predefined operational meaning. as an example, operator + is employed to feature two integers also as join two strings and merge two lists. it&#8217;s achievable because \u2018+\u2019 operator is overloaded by int class and stir class. you&#8217;d possibly have noticed that an equivalent built-in operator or function shows different behavior for objects of various classes, this is often called Operator Overloading.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to show use of <br\/># + operator for different purposes. <br\/>  <br\/>print(1 + 2) <br\/>  <br\/># concatenate two strings <br\/>print(&quot;Wiki&quot;+&quot;techy&quot;)  <br\/>  <br\/># Product two numbers <br\/>print(3 * 4) <br\/>  <br\/># Repeat the String <br\/>print(&quot;Wiki&quot;*4) <\/code><\/pre> <\/div>\n<h3 id=\"output\"><strong>Output:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">3<br\/>Wikitechy<br\/>12<br\/>WikiWikiWikiWiki<\/code><\/pre> <\/div>\n<h2 id=\"how-to-overload-the-operators-in-python\"><strong>How to overload the operators in Python?<\/strong><\/h2>\n<p><strong>\u00a0<\/strong>Consider that we&#8217;ve two objects which are a physical representation of a category (user-defined data type) and that we need to add two objects with binary \u2018+\u2019 operator it throws a mistake, because compiler don\u2019t skills to feature two objects. So, we define a way for an operator which process is named operator overloading. we will overload all existing operators but we can\u2019t create a replacement operator. To perform operator overloading, Python provides some special function or magic function that&#8217;s automatically invoked when it&#8217;s related to that specific operator. for instance, once we use + operator, the magic method __add__ is automatically invoked during which the operation for + operator is defined.<\/p>\n<h2 id=\"overloading-binary-operator-in-python\"><strong>Overloading binary + operator in Python?<\/strong><\/h2>\n<p>When we use an operator on user defined data types then automatically a special function or magic function related to that operator is invoked. Changing the behavior of operator is as simple as changing the behavior of method or function. You define methods in your class and operators work consistent with that behavior defined in methods. once we use + operator, the magic method __add__ is automatically invoked during which the operation for + operator is defined. There by changing this magic method\u2019s code, we&#8217;ll give extra going to the + operator.<\/p>\n<h3 id=\"code-1\"><strong>Code 1:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python Program illustrate how  <br\/># to overload an binary + operator <br\/>  <br\/>class A: <br\/>    def __init__(self, a): <br\/>        self.a = a <br\/>  <br\/>    # adding two objects  <br\/>    def __add__(self, o): <br\/>        return self.a + o.a  <br\/>ob1 = A(1) <br\/>ob2 = A(2) <br\/>ob3 = A(&quot;Wiki&quot;) <br\/>ob4 = A(&quot;techy&quot;) <br\/>  <br\/>print(ob1 + ob2) <br\/>print(ob3 + ob4) <\/code><\/pre> <\/div>\n<h3 id=\"output-2\"><strong>Output:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">3<br\/>Wikitechy<\/code><\/pre> <\/div>\n<h3 id=\"code-2\"><strong>Code 2:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python Program to perform addition  <br\/># of two complex numbers using binary  <br\/># + operator overloading. <br\/>  <br\/>class complex: <br\/>    def __init__(self, a, b): <br\/>        self.a = a <br\/>        self.b = b <br\/>  <br\/>     # adding two objects  <br\/>    def __add__(self, other): <br\/>        return self.a + other.a, self.b + other.b <br\/>  <br\/>    def __str__(self): <br\/>        return self.a, self.b <br\/>  <br\/>Ob1 = complex(1, 2) <br\/>Ob2 = complex(2, 3) <br\/>Ob3 = Ob1 + Ob2 <br\/>print(Ob3) <\/code><\/pre> <\/div>\n<h3 id=\"output-3\"><strong>Output:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">(3, 5)<\/code><\/pre> <\/div>\n<h2 id=\"overloading-comparison-operators-in-python\"><strong>Overloading comparison operators in Python :<\/strong><\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to overload equality <br\/># and less than operators <br\/>  <br\/>class A: <br\/>    def __init__(self, a): <br\/>        self.a = a <br\/>    def __lt__(self, other): <br\/>        if(self.a&lt;other.a): <br\/>            return &quot;ob1 is lessthan ob2&quot;<br\/>        else: <br\/>            return &quot;ob2 is less than ob1&quot;<br\/>    def __eq__(self, other): <br\/>        if(self.a == other.a): <br\/>            return &quot;Both are equal&quot;<br\/>        else: <br\/>            return &quot;Not equal&quot;<br\/>                  <br\/>ob1 = A(2) <br\/>ob2 = A(3) <br\/>print(ob1 &lt; ob2) <br\/>  <br\/>ob3 = A(4) <br\/>ob4 = A(4) <br\/>print(ob1 == ob2) <\/code><\/pre> <\/div>\n<h3 id=\"output-4\"><strong>Output:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">ob1 is lessthan ob2<br\/>Not equal<\/code><\/pre> <\/div>\n<h2 id=\"overloading-equality-and-less-than-operators\"><strong>Overloading equality and less than operators :<\/strong><\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to overload equality <br\/># and less than operators <br\/>  <br\/>class A: <br\/>    def __init__(self, a): <br\/>        self.a = a <br\/>    def __lt__(self, other): <br\/>        if(self.a&lt;other.a): <br\/>            return &quot;ob1 is lessthan ob2&quot;<br\/>        else: <br\/>            return &quot;ob2 is less than ob1&quot;<br\/>    def __eq__(self, other): <br\/>        if(self.a == other.a): <br\/>            return &quot;Both are equal&quot;<br\/>        else: <br\/>            return &quot;Not equal&quot;<br\/>                  <br\/>ob1 = A(2) <br\/>ob2 = A(3) <br\/>print(ob1 &lt; ob2) <br\/>  <br\/>ob3 = A(4) <br\/>ob4 = A(4) <br\/>print(ob1 == ob2) <\/code><\/pre> <\/div>\n<h3 id=\"output-5\"><strong>Output:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">ob1 is lessthan ob2<br\/>Not equal<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Operator Overloading means giving extended meaning beyond their predefined operational meaning. as an example, operator + is employed to feature two integers also as join two strings and merge two lists. it&#8217;s achievable because \u2018+\u2019 operator is overloaded by int class and stir class. you&#8217;d possibly have noticed that an equivalent built-in operator or function [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":33604,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4148],"tags":[86904,86905,86903],"class_list":["post-33515","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-python","tag-comparison-in-python","tag-equality-and-less-than-operators","tag-overloading-in-python"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=33515"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33515\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/33604"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=33515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=33515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=33515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}