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’s achievable because ‘+’ operator is overloaded by int class and stir class. you’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.

[pastacode lang=”python” manual=”%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″ message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”3%0AWikitechy%0A12%0AWikiWikiWikiWiki” message=”” highlight=”” provider=”manual”/]

How to overload the operators in Python?

 Consider that we’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 ‘+’ operator it throws a mistake, because compiler don’t 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’t create a replacement operator. To perform operator overloading, Python provides some special function or magic function that’s automatically invoked when it’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.

Overloading binary + operator in Python?

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’s code, we’ll give extra going to the + operator.

Code 1:

[pastacode lang=”python” manual=”%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″ message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”3%0AWikitechy” message=”” highlight=”” provider=”manual”/]

Code 2:

[pastacode lang=”python” manual=”%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″ message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”(3%2C%205)” message=”” highlight=”” provider=”manual”/]

Overloading comparison operators in Python :

[pastacode lang=”python” manual=”%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″ message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”ob1%20is%20lessthan%20ob2%0ANot%20equal” message=”” highlight=”” provider=”manual”/]

Overloading equality and less than operators :

[pastacode lang=”python” manual=”%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″ message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”ob1%20is%20lessthan%20ob2%0ANot%20equal” message=”” highlight=”” provider=”manual”/]

Categorized in: