It’s better to say instantiate Team("Utah Jazz"), i.e. we create an object whose type is Team which will take as a defining feature the string Utah Jazz.
Let’s think about a more familiar example: int("3"). As you probably know, this creates an integer, namely 3. When you run a script whose only line of code is int("3"), it creates the object and that’s it.
This is important because comparison doesn’t make sense unless you have two arguments to compare.
Now let’s instead create two integers and compare them.
So the comparison operators really are methods. More on this in the docs.
The other parameter is whatever inside the parentheses. I the examples above other is respectively the values of y, y and x. And below it is the number 1337.
Where as self is the object whose method we’re using.
The goal of screens 7 and 8 is to show that the meaning of <, == and so on can be modified (or overloaded, or overridden, or overloaded, whatever you prefer).
On screen 7 we read the following.
These methods already exist in the object class by default, and we’ve used these operators to compare integers, floating point numbers (decimals), and strings. The operators work because classes like string have implemented them specifically.
Since the class Team was created from object, it will inherit its methods. Since the comparison operators are methods in object, they are inherited from this class.
On screen 8 we’re tasked with redefining these methods in a way that team_1 □ team_2 compares (in the usual sense) the average age of team_1 with the average age of team_2, where □ can be any of the typical comparison operators: <, == and so on.
The current implementation keeps an array of integer objects for all integers between -5 and 256 , when you create an int in that range you actually just get back a reference to the existing object.
Whenever you have an integer value between -5 and 256 inclusively, the reference is always the same to the integer of the same value between -5 and 256 inclusively. That is, there only exists one memory location or reference to integer belong to the range -5 and 256 inclusively.
Common pitfalls are when you use is operator to check for values between integers.
If you use the is operator to compare two integers, a comparison will always equate to true when the value is the same but within the range from -5 to 256 inclusively.
>>> x = 256
>>> y = int("256")
>>> x is y
If you use the is operator to compare two integers, a comparison will always equate to false when the value is the same but outside of the range from -5 to 256.
>>> x = 257
>>> y = int("257")
>>> x is y
Therefore, use == equality operator to compare the value between two integers.