{"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\u2019s achievable because \u2018+\u2019 operator is overloaded by int class and stir class. you\u2019d 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[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20show%20use%20of%20%0A%23%20%2B%20operator%20for%20different%20purposes.%20%0A%20%20%0Aprint(1%20%2B%202)%20%0A%20%20%0A%23%20concatenate%20two%20strings%20%0Aprint(%22Wiki%22%2B%22techy%22)%20%20%0A%20%20%0A%23%20Product%20two%20numbers%20%0Aprint(3%20*%204)%20%0A%20%20%0A%23%20Repeat%20the%20String%20%0Aprint(%22Wiki%22*4)%20\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\"><strong>Output:<\/strong><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d3%0AWikitechy%0A12%0AWikiWikiWikiWiki\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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\u2019ve 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\u2019s automatically invoked when it\u2019s 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\u2019ll give extra going to the + operator.<\/p>\n<h3 id=\"code-1\"><strong>Code 1:<\/strong><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20Program%20illustrate%20how%20%20%0A%23%20to%20overload%20an%20binary%20%2B%20operator%20%0A%20%20%0Aclass%20A%3A%20%0A%20%20%20%20def%20__init__(self%2C%20a)%3A%20%0A%20%20%20%20%20%20%20%20self.a%20%3D%20a%20%0A%20%20%0A%20%20%20%20%23%20adding%20two%20objects%20%20%0A%20%20%20%20def%20__add__(self%2C%20o)%3A%20%0A%20%20%20%20%20%20%20%20return%20self.a%20%2B%20o.a%20%20%0Aob1%20%3D%20A(1)%20%0Aob2%20%3D%20A(2)%20%0Aob3%20%3D%20A(%22Wiki%22)%20%0Aob4%20%3D%20A(%22techy%22)%20%0A%20%20%0Aprint(ob1%20%2B%20ob2)%20%0Aprint(ob3%20%2B%20ob4)%20\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-2\"><strong>Output:<\/strong><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d3%0AWikitechy\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"code-2\"><strong>Code 2:<\/strong><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20Program%20to%20perform%20addition%20%20%0A%23%20of%20two%20complex%20numbers%20using%20binary%20%20%0A%23%20%2B%20operator%20overloading.%20%0A%20%20%0Aclass%20complex%3A%20%0A%20%20%20%20def%20__init__(self%2C%20a%2C%20b)%3A%20%0A%20%20%20%20%20%20%20%20self.a%20%3D%20a%20%0A%20%20%20%20%20%20%20%20self.b%20%3D%20b%20%0A%20%20%0A%20%20%20%20%20%23%20adding%20two%20objects%20%20%0A%20%20%20%20def%20__add__(self%2C%20other)%3A%20%0A%20%20%20%20%20%20%20%20return%20self.a%20%2B%20other.a%2C%20self.b%20%2B%20other.b%20%0A%20%20%0A%20%20%20%20def%20__str__(self)%3A%20%0A%20%20%20%20%20%20%20%20return%20self.a%2C%20self.b%20%0A%20%20%0AOb1%20%3D%20complex(1%2C%202)%20%0AOb2%20%3D%20complex(2%2C%203)%20%0AOb3%20%3D%20Ob1%20%2B%20Ob2%20%0Aprint(Ob3)%20\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-3\"><strong>Output:<\/strong><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d(3%2C%205)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h2 id=\"overloading-comparison-operators-in-python\"><strong>Overloading comparison operators in Python :<\/strong><\/h2>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20overload%20equality%20%0A%23%20and%20less%20than%20operators%20%0A%20%20%0Aclass%20A%3A%20%0A%20%20%20%20def%20__init__(self%2C%20a)%3A%20%0A%20%20%20%20%20%20%20%20self.a%20%3D%20a%20%0A%20%20%20%20def%20__lt__(self%2C%20other)%3A%20%0A%20%20%20%20%20%20%20%20if(self.a%3Cother.a)%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22ob1%20is%20lessthan%20ob2%22%0A%20%20%20%20%20%20%20%20else%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22ob2%20is%20less%20than%20ob1%22%0A%20%20%20%20def%20__eq__(self%2C%20other)%3A%20%0A%20%20%20%20%20%20%20%20if(self.a%20%3D%3D%20other.a)%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Both%20are%20equal%22%0A%20%20%20%20%20%20%20%20else%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Not%20equal%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0Aob1%20%3D%20A(2)%20%0Aob2%20%3D%20A(3)%20%0Aprint(ob1%20%3C%20ob2)%20%0A%20%20%0Aob3%20%3D%20A(4)%20%0Aob4%20%3D%20A(4)%20%0Aprint(ob1%20%3D%3D%20ob2)%20\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-4\"><strong>Output:<\/strong><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201dob1%20is%20lessthan%20ob2%0ANot%20equal\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h2 id=\"overloading-equality-and-less-than-operators\"><strong>Overloading equality and less than operators :<\/strong><\/h2>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20overload%20equality%20%0A%23%20and%20less%20than%20operators%20%0A%20%20%0Aclass%20A%3A%20%0A%20%20%20%20def%20__init__(self%2C%20a)%3A%20%0A%20%20%20%20%20%20%20%20self.a%20%3D%20a%20%0A%20%20%20%20def%20__lt__(self%2C%20other)%3A%20%0A%20%20%20%20%20%20%20%20if(self.a%3Cother.a)%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22ob1%20is%20lessthan%20ob2%22%0A%20%20%20%20%20%20%20%20else%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22ob2%20is%20less%20than%20ob1%22%0A%20%20%20%20def%20__eq__(self%2C%20other)%3A%20%0A%20%20%20%20%20%20%20%20if(self.a%20%3D%3D%20other.a)%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Both%20are%20equal%22%0A%20%20%20%20%20%20%20%20else%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Not%20equal%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0Aob1%20%3D%20A(2)%20%0Aob2%20%3D%20A(3)%20%0Aprint(ob1%20%3C%20ob2)%20%0A%20%20%0Aob3%20%3D%20A(4)%20%0Aob4%20%3D%20A(4)%20%0Aprint(ob1%20%3D%3D%20ob2)%20\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-5\"><strong>Output:<\/strong><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201dob1%20is%20lessthan%20ob2%0ANot%20equal\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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\u2019s achievable because \u2018+\u2019 operator is overloaded by int class and stir class. you\u2019d 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}]}}