Welcome to your next lesson in Object-Oriented Programming in Python versus Java. In this lesson, we will take a look at operator overloading. You saw in your last lesson that Python provides a lot of built-in methods that objects inherit from Python’s
00:20 Many of those are to overload common symbols used for typical operations.
Remember that in Java, you need to use method calls for very basic tasks. So if you want to check two objects for equality, you have to write an
.equals() method for that class.
If you want to perform arithmetic on non-built-in numbers, you have to create an
.add() method. If you want to compare two objects that you’ve created, you need to implement an interface
Comparable, and then write a
And the syntax is
<object>.<methodCall>(), and then in parentheses the other object. The calling object becomes the left-hand operand and the right-hand operand, we pass as a parameter.
01:13 The point is you can’t use the best symbols for those tasks. There are these awkward looking method calls.
01:23 Python, on the other hand, allows you to overload many operators by writing a specific dunder method for whichever operator you would like to use.
This is just a short list, but you get comparison operators such as equal to (
==) and not equal to (
!=), you get all four of the less than (
<), greater than (
>), less than or equal to (
<=), greater than or equal to (
>=). Arithmetic: add (
+), subtract (
-), multiply (
*), divide (
/), modulus (
%), and others.
And so if you want to use the is equal to symbol,
==, to compare two objects of your class, write an appropriate dunder method,
.__eq__(). That method will allow you to use the
== operator to compare two of your objects. Similarly, if you want to compare using is not equal to, you would write a
.__ne__() dunder method. Less than would be
.__lt__(), less than or equal to would be
.__le__(), greater than is
.__gt__(), and so on and so forth. Something to keep in mind: if you’re going to overload the
== operator, that doesn’t automatically overload the
Now, you will probably—if you want to—define that just to return the negation of what your
== operator does,
02:51 so some of those do not require a lot of new writing.
So, in my
Car class, I have created an
.__eq__() method. It takes two parameters.
self represents the left-hand operand,
other represents the right-hand operand.
And this is typically what I use for an is equal to method. It matches up with the
.equals() method that I would write in Java. I would check to see for referential equality—if they were the same object, then definitely we would return
True. Java has
instanceof, Python has
isinstance(). If these two items are not of the same class, then they can’t possibly be equal to each other.
And then the third case, if we get here, then we will check for value equality and check to see if the values of the attributes match up perfectly. And again, because the operators have been overloaded for built-in types, strings already have the
== operator and we don’t have to use the
.equals() method, as we do for Java.
And again, because I have written the
.__eq__() method, I can write as an
if car1 == car2. And if they are equal—the way I’ve written mine, either referential equality or value equality, if either of those hold—then I would proceed in to the
And if that condition failed, I would go to an
else if it were there. But the point is, I can use the
== operator. And again,
car1 would be used for
car2 would be used for
04:47 And that finishes our series of lessons over inheritance and polymorphism. In our next lesson, we’ll take a look at reflection: how an object can learn information about itself.
Become a Member to join the conversation.