Everything in the Python program is an interaction between different entities.
For example, we have libraries such as pandas.
We have statements such as import.
The return statement that if statement, the statement we have keywords such as the class keywords,
def keywords.
Then we have classes such as this block of code here.
That's an entire class.
Then we had instances.
This year is an instance of a hotel class.
That's another instance of.
The hotel class as well.
This string here is an instance of a string class.
This other string is also an instance.
Or for string class.
Then we had.
Functions.
Which are these outside of classes?
And then we have methods which are like functions, but they are inside classes.
Such as this method here and that method there.
So a Python program consists of interaction between things like functions and instances and methods.
Even when you do things like ease the hello instance, the hello string instance equal to the high string
instance.
Even when you do that, what you're actually doing is you're calling a method.
In this case it would be this method underscore, underscore, e, q, underscore, underscore.
Hi.
And you get the same output.
Actually, you are not the one who are calling this method.
The interpreter is calling this method on the background.
So this is implicitly called and this here is known as syntactic sugar.
You can think of syntactic sugar as simplified syntax so that you don't have to write that methods you
can just do.
Hello.
Is equal to high.
So that's more readable basically.
Other examples would be one plus two.
That would be the equivalent of one space dot.
You have to use a space with numbers because it can be confused.
Python doesn't doesn't know if it's a float or an integer if you don't provide space.
So that would be add to and you get three.
So when you do that, you're calling the ADD method.
Actually now you can see a list of magic methods when you use there.
For example, dear hello or just dear store.
And you'll see the methods available for STR.
Similarly, we can check for magic methods that our classes possess because STR is a class, right?
Hotel is also a class.
So let's copy this class in the python castle.
You can either copy the entire class or.
You can improve that class.
But first, I'd like to remove all these.
Let's just keep the definition of the classes here, save the script, and then from main, that's main
to main to import the hotel class.
Now hotel is available as a name in this Python session.
And I can do Dear Hotel and you'll see that the hotel class has all these magic methods.
Among the methods we will see.
Underscore underscore e.
Q So as you saw, that was the equivalent of this operator.
So let's try to use this operator with two instances of the hotel class, right?
Just like we used here, that operator with two instances of the string class.
So hello is an instance of a string class.
How is also an instance of string class?
So let's do the same here.
Let's create two instances.
First, that would be hotel.
You can either store them in variables or you can compare them directly.
I'll store them in variables.
So hotel.
What do we need to provide?
We need to provide the hotel ID.
I'll just pass directly the value without mentioning the arguments you can do either way.
So that would be.
188 rate 188.
I'll do another hotel instance and I'll provide the same.
I'd actually.
Still, these are treated as two different instances by Python.
So if you do hotel, one is equal to hotel two, you get false.
Why?
Well, because these are basically treated as two different objects.
And you can keep creating more hotel instances using the same ID, you can create ten of them and you
can compare any of them, and all of them will be different from each other.
That is not the case with things like strings, right?
Hello is equal to hello and we get true.
That is because the class string, when it was defined by some developers in the code developer team
or the Python organization, they have overwritten the ECC methods.
So this method and we can do the same so that when we compare hotel one and hotel two, if they have
the same ID, we will return false.
That should return false.
So let's do that in our code.
Let's overwrite that.
E q methods.
So we will change the behavior of that method.
So let's go to the hotel class.
Normally you add magic methods at the end, so after the last method, but make sure to be inside the
class and write, define, underscore, underscore e Q The name has to be the same.
Exactly the same.
You'll see this gets this special color in Python.
And now you want to specify how do you want this method to behave?
So when do you want to treat two instances as being the same as being equal?
Well, I think if self taught.
Hotel.
So this idea is.
Equal to self dot hotel ID.
Then we return true.
Else we return false.
Save the script.
Then make sure to close this python session.
You need to reopen a python session again.
Otherwise you will be working on the old class which doesn't have the queue.
Sorry, the queue method.
So import in a new python session.
Import from main to import hotel again.
Create two instances hotel one with hotel.
188.
A hotel to Ace Hotel 188.
Now, Hotel one is equal to Hotel two.
Initially.
We're going to get an error.
And I'm glad this error happened because now you can understand a key concept here.
So let's read the error.
It says hotel IQ takes one positional argument, but two were given.
So hotel IQ takes one positional arguments, which is true, right?
It's only takes self a port.
Two arguments were given here.
What is the second argument that we've given to this IQ methods?
Well, the second argument is the second instance, right.
Because this here is equivalent of.
Doing hotel.
Actually, it's the equivalent of doing hotel.
The class dot e, Q underscore underscore Hotel one, Hotel two.
That's what we're doing.
So we should reflect that syntax in here.
So we should go there and add something like other.
And then self should be updated to other here.
So basically we are comparing.
This current instance hotel one with this other instance which is passed as the second argument hotel
to in this case.
So hotel to that ID and so on.
Right.
So save that script.
Close that Python console imports Again, you can use the upper arrow key to call previously executed
statements.
Import hotel creates a hotel.
One instance creates a hotel, two instance with the upper arrow key.
Compare the two.
It should be this one and we get true.
So now we can successfully compare two instances and evaluate them as true if they have the same idea.
So this could be useful in your program.
If you want to expand your program, for example, and you want to see if two hotel instances are the
same.
So to do that, you can just compare them like that, like you do with other.
Instances.
Types such as strings or numbers.
So similarly, you can add more magic methods.
You can override more magic methods here.
Perhaps it makes sense to add to one hotel to another.
So if that makes sense for your program, then you want to write exactly the behavior of what should
happen when you add those two hotels together.
So perhaps you want to access self that price.
If you had a price instance variable, which we don't have in this case, but if you had one, you would
do something like sell that price plus other that price.
So you could.
Add them at a total in a total variable, you could store them at other here and then return total.
And then when you do hotel one plus hotel two, you would get the total price as.
Output.
So that would be a number.
So I'm deleting that and that just to show you how it would be if you use the add magic method.
So that was some magic about Python and you can try to experiment with more methods if you want to have
some python fun.
And I'll leave this lecture at this point.
I hope this makes sense and it gives you some more advanced knowledge about how Python works, the inner
works of Python.
And yeah, I'll talk to you in the next videos.
Thanks a lot for following.
I'm very happy to have you here.
See you.