Python Operator Overloading

Operator overloading is an advanced technique we can use to make classes comparable and to make them work with Python operators.

Let’s take a class Dog:

class Dog:
    # the Dog class
    def __init__(self, name, age):
        self.name = name
        self.age = age

Let’s create 2 Dog objects:

roger = Dog('Roger', 8)
syd = Dog('Syd', 7)

We can use operator overloading to add a way to compare those 2 objects, based on the age property:

class Dog:
    # the Dog class
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __gt__(self, other):
        return True if self.age > other.age else False

Now if you try running print(roger > syd) you will get the result True.

In the same way we defined __gt__() (which means greater than), we can define the following methods:

  • __eq__() to check for equality
  • __lt__() to check if an object should be considered lower than another with the < operator
  • __le__() for lower or equal (<=)
  • __ge__() for greater or equal (>=)
  • __ne__() for not equal (!=)

Then you have methods to interoperate with arithmetic operations:

  • __add__() respond to the + operator
  • __sub__() respond to the operator
  • __mul__() respond to the * operator
  • __truediv__() respond to the / operator
  • __floordiv__() respond to the // operator
  • __mod__() respond to the % operator
  • __pow__() respond to the ** operator
  • __rshift__() respond to the >> operator
  • __lshift__() respond to the << operator
  • __and__() respond to the & operator
  • __or__() respond to the | operator
  • __xor__() respond to the ^ operator

There are a few more methods to work with other operators, but you got the idea.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Operator overloading is an advanced technique we can use to make classes comparable and to make them work with Python operators.

Let’s take a class Dog:

class Dog:
    # the Dog class
    def __init__(self, name, age):
        self.name = name
        self.age = age

Let’s create 2 Dog objects:

roger = Dog('Roger', 8)
syd = Dog('Syd', 7)

We can use operator overloading to add a way to compare those 2 objects, based on the age property:

class Dog:
    # the Dog class
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __gt__(self, other):
        return True if self.age > other.age else False

Now if you try running print(roger > syd) you will get the result True.

In the same way we defined __gt__() (which means greater than), we can define the following methods:

  • __eq__() to check for equality
  • __lt__() to check if an object should be considered lower than another with the < operator
  • __le__() for lower or equal (<=)
  • __ge__() for greater or equal (>=)
  • __ne__() for not equal (!=)

Then you have methods to interoperate with arithmetic operations:

  • __add__() respond to the + operator
  • __sub__() respond to the operator
  • __mul__() respond to the * operator
  • __truediv__() respond to the / operator
  • __floordiv__() respond to the // operator
  • __mod__() respond to the % operator
  • __pow__() respond to the ** operator
  • __rshift__() respond to the >> operator
  • __lshift__() respond to the << operator
  • __and__() respond to the & operator
  • __or__() respond to the | operator
  • __xor__() respond to the ^ operator

There are a few more methods to work with other operators, but you got the idea.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-02-21T05:00:00+00:00) Python Operator Overloading. Retrieved from https://www.scien.cx/2021/02/21/python-operator-overloading/

MLA
" » Python Operator Overloading." flaviocopes.com | Sciencx - Sunday February 21, 2021, https://www.scien.cx/2021/02/21/python-operator-overloading/
HARVARD
flaviocopes.com | Sciencx Sunday February 21, 2021 » Python Operator Overloading., viewed ,<https://www.scien.cx/2021/02/21/python-operator-overloading/>
VANCOUVER
flaviocopes.com | Sciencx - » Python Operator Overloading. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/21/python-operator-overloading/
CHICAGO
" » Python Operator Overloading." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/02/21/python-operator-overloading/
IEEE
" » Python Operator Overloading." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/02/21/python-operator-overloading/. [Accessed: ]
rf:citation
» Python Operator Overloading | flaviocopes.com | Sciencx | https://www.scien.cx/2021/02/21/python-operator-overloading/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.