Here's our CashRegister class. We've removed the examples in order to
fit more of this on the screen at once. We have our special method underscores in
it. As you know now, this gets called when
you create a new CashRegister object. We also have regular methods get_total,
add and remove. We're going to add another special
method, underscore zq that will be called when we compare 2 cash registers for
equality. For this method, we'll consider 2 cash
registers to be equal if they have the same total amount of money.
Our method has self as the first parameter as per usual.
And the second variable is the other cash register that we're comparing this cash
register to. In our dock test, we'll create one cash
register with 2 loonies, that's $2 total, and we'll create a second cash register
with 1 toonie, also $2. That means, that when we compare them, we
hope that the result is true for the type contract self and other both refer to the
cash registers and this method should produce a Boolean, in particular, this
method will return true if and only if self and other have the same total amount
of money. In order to decide if 2 cash registers
are [UNKNOWN], we've decided to compare the total amount of money.
So self.gettotal should produce the total amount of money in the cash register.
And we want to know if that number is equal to others total.
Let's go modify the main program to try this out.
We'll remove the old code and then create three CashRegister objects.
The first one, has two loonies, the second one has one toonie and the third
one has a loonie and a toonie. Let's print what happens when we compare
the first one to the second one and the third one to the second one.
When we've done this we see that the first expression produces true and the
second one produces false. Let's step through this of the debugger
so we can see exactly what's going on now that the debugger is turned on when we
run. We pause right at the very top of the
file that we're running. We define our class.
Notice, now that we've read the entire class definition, including any methods.
In the debug control window, we see that we're paused on line 78 of
CashRegister.pi. And that we're about to execute if
underscores name equals the string underscores main.
Down in the locals section of the debug control window we see that variable
underscore's name has, as its value, the string, underscores main.
So this condition in the if statement will evaluate to true.
Here we confirm that, we're about to create a cash register and assign the
memory address of that to variable CR1. We'll create the next two cash register
objects as well, assigning them to variable CR2 and CR3.
We've been clicking button over, which executes the current statement in its
entirety, all at once, without showing any intermediate steps.
We could instead, if we like click start, which steps into any methods or functions
that get called. We're going to do that here.
Now, if you'll recall, we [UNKNOWN] an underscore zq method in order to hook
into the Python syntax for the equal equals symbol, the comparison for
equality. So when I click step we will actually be
taken into that method, here we go. We are currently on line 27 of
CashRegister.pi and method under score zq, and we see that also on the call
stack that in the main module on line 83, the call, the print is what got us inside
this eq method. I can click here, in order to see the
variables, and their values, in the global scope.
And I can click here so that I can examine what's going on inside method eq.
What I see in the local screen of the debug control window is that self has as
its value, a CashRegister object. And it's at this funny memory address as
a reminder that's a hexadecimal number, it uses the digits zero through nine,
plus a through f. So self is the cash register object at
this particular memory address that ends in e 5 0.
Let's go back up and remember e50 to see which one we are using that is cr1.
So cr1 was sent in as self. Which one is 850?
Which one is the other one? That is cr2, hopefully, as we expect.
If you recall, the total amount of money in the first cash register was $2.
And the total amount of money in the second cash register was also $2.
So, when we execute this return statement here, to compare the the two total values
for self and other. We hope that they compare true, and
indeed when I step here, I see that I'm going to add up the loonies, toonies,
fives, tens, and 20s for the correct cash register which as you can see is the one
that memory addressed e50, that's cash register one And when I click step again
it's going to return that value and then call get total on the other cash
register. Here we go, we're back in get total, but
this time for the other CashRegister. Notice the memory address, clicking once
more will go back to the self.gettotal is equal to other dot gettotal statement,
which should produce true. It will also take us back to the print
statement on line 83, here we go. When I step again, it's going to call the
underscore eq method for cr3, whose memory address ends in, f50.
Here we go. Self, refers to the same object, that cr3
refers to, other refers to the same object that cr2 refers to.
Notice that the object that owns this underscore is eq method, is the one that
appears in the left hand side of the comparison.
When we step, we're taking the cr3's yet total method, when we step again, we're
taking under cr2's get total method. So at this point, we've called
self.get_total, and we're in midst of executing other .get_total.
When I step again, we're going to exit other.get_total, we're going to do the
comparison and return to the main program where we started this whole process.
Here we go, you'll notice that we don't see anything.
That is because the output appears up here in the Python shell, where it always
has. We're going to do one more thing
involving equality. Let's go back up to our eq method, and
comment it out. We're going to explore now what happens
when we compare two variables for which there is no underscore zq method to find.
Here, when we run this, ee see that both expressions produce false.
That is because Python by default compares the memory addresses of the
objects for equality. If there is no underscores eq method to
find in the class it uses objects eq method that we've inherited.
And that method simply compares memory addresses.
Anytime you write your own class, you will need to decide for yourself what it
means to compare true instances of your class.
Here, we can decide that two CashRegister objects are equal and their totals are
equal. Perhaps we could have been stricter and
said two CashRegister objects are equal when they have the same number of loonies
and the same number of toonies and so on, or we may decide that two CashRegister
objects are equal only if they are indeed the same exact object at the same memory
address.