Thumbnail for Object equality | Intro to CS - Python | Khan Academy by Khan Academy

Object equality | Intro to CS - Python | Khan Academy

Khan Academy

5m 35s811 words~5 min read
YouTube auto captions
Transcript source

YouTube auto captions

This transcript was extracted from YouTube's auto-generated caption track. The transcript below is server-rendered so it can be read, searched, cited, and shared without opening the original YouTube player.

Pull quotes
[0:00]Now, with your human brain, you'd probably say a queen of hearts is equal to any other queen of hearts.
[0:00]If I'm playing a matching game, then it only matters if the ranks and suits are the same, even if those cards came from different decks.
[0:00]But if I'm doing a magic trick where you signed this particular card, you probably wouldn't be too impressed if I just produced a different queen of hearts.
[0:00]When we define a class, Python can't safely assume things about the type's attributes.
Use this transcript
Related transcript hubs

[0:00]Are these objects equal? By default, Python compares objects based on identity. That means two objects are equal only if they're literally the same object. So, this card object is only ever equal to itself. It has to be the exact same object in memory. Now, the question is, is it even useful to compare objects this way? Well, my useful answer is that it depends. Let's rewind for a second. A card object represents a standard playing card. It has two attributes, a rank and a suit. For example, this card has a rank queen and a suit hearts. Now, with your human brain, you'd probably say a queen of hearts is equal to any other queen of hearts. But it kind of depends what you're using those cards for. If I'm playing a matching game, then it only matters if the ranks and suits are the same, even if those cards came from different decks. But if I'm doing a magic trick where you signed this particular card, you probably wouldn't be too impressed if I just produced a different queen of hearts. It's only equal if it's the same exact card. When we define a class, Python can't safely assume things about the type's attributes. Since we can technically add or delete attributes after an object's created, that's why it defaults to comparing objects based on identity. But Python gives us as the programmer, the option to override that behavior, using the equals method. The equals method is a special method that we can add to our class definitions. It tells Python how to compare two objects of that type. Python will automatically execute this equals method anytime we compare a card object, meaning when we use the equals equals operator. Passing in that first object for the first parameter and the second object we're comparing for the second parameter. Let's break this down. Right now, when I compare these two cards, Python says they're not equal. But the behavior I actually want is for Python to consider two cards equal if they have the same rank and the same suit. So, I can go to my card class definition and add the special method underscore underscore EQ underscore underscore. This is the name for the equals method, and for Python to find it, it needs to match exactly. It also always needs to take two parameters, which represent the two objects being compared. By convention, we name the first parameter self and the second parameter other. The equals method should always return a boolean value, true if the objects are equal and false if they're not. For now, let's hard code it to always return true. And for visibility, I'll add a temporary print statement. If I run the program again, we can see that our equals method is getting executed. And now it says that these objects are equal. When Python sees an equals equals operator with a card object on the left hand side, it automatically executes the card object's equals method, if it has one. The comparison expression evaluates to whatever value that equals method returns. So, since it always returns true right now, no matter which two card objects I compare, Python will always say they're equal. That's not the behavior I wanted. So, let's go back and change our equals method. Let's check if the rank attribute of the first object is equal to the rank attribute of the other object, and return that boolean result. Now, when I run this program, it says that my eight of spades and queen of hearts are not equal. Because Python's comparing based on that rank attribute. But if I compare a queen of spades and a queen of hearts, Python says that the objects are equal. Because they both have the same rank queen. For some programs, the suit might be irrelevant, so this would be the exact behavior that we want. But for my use case, I wanted both the rank and the suit to match. So, one last change to our equals method. Let's also compare the suit attribute. And now, Python says my queen of spades and queen of hearts are not equal. But these two different queen of hearts cards are. And just to drive that home, if I go back into my class definition and delete that equals method, Python will default back to comparing objects by identity. So, since these are two different queen of hearts cards, Python says they're not equal. But if I add that equals method back in, Python uses that logic to decide if two cards are equal. So, while these aren't the same exact object in memory, according to our special equals method, since their ranks are the same and their suits are the same, these two card objects should be considered equal.

Need another transcript?

Paste any YouTube URL to get a clean transcript in seconds.

Get a Transcript