Hey, welcome back.
In this video, you learn how to create class variables.
You have learned already how to create instance variables.
So we have instance variables and we have class variables.
I'll mention the difference between the two and show you what each of these are.
We're going to work on the hotel booking app we've been building so far, but I'll simplify the main
pie script a little bit.
Actually, I'll just make a copy of this file, so please do the same.
I'll name the second copy main to that P and then in main to that py, I'll simplify the code a little
bit so I'll remove.
The two classes, credit card and secure credit card.
I'll remove the entire loop where we run the program, and I'll keep only the two classes, reservation
ticket and hotel.
And also these two lines are pointing to the cards that CSV file.
So I'll remove those too.
Now I'm also attaching this script to the lecture resources.
So if you want to make sure that you have the same code as me, you can just copy that file and paste
it in your project directory and name it with something like Main two PI.
So instance variables versus class variables.
You have already created instance variables.
Instance variables are these.
Hotel ID.
Name for the hotel instances, customer name and hotel for the reservation.
Ticket instances.
Now let me create a class variable and then explain you the difference between a class variable and
an instance variable.
Class variables are coded outside the methods of a class instance.
Variables are coded inside a method.
Usually it's the init method.
But class variables are coded here.
For example, let's say watermark is equal to, let's say, the real estate company.
This is the company that has this app, this hotel booking app.
So they are a company who offer hotels for holidays for people who go to vacation and so on.
So that means now this text is basically part of the class.
It's not specific to a particular hotel, Right.
That's why it is a class variable.
Let me try to explain it better by creating some instances of the hotel class.
So let's say hotel one is hotel ID is equal to.
So this expects an ID actually its hotel ID, hotel ID is equal to.
Well, let's give one of these IDs.
Let's say 188 188.
Let's create another a second instance.
Hotel, two hotel, hotel ID that's equal to.
134.
Make sure to give them as strings because we are loading the ID field as a string here, the ID fields.
These are read as strings inside Python.
So 134 right now if I print out hotel one dot name.
And I execute the script.
I'll get Snow Palace, right.
That's the value of the name instance variable.
So this is a variable of this particular instance.
And now if I do print hotel to that name.
We'll get to sunny apartment.
So because name gives us different values for different instances, that's why we call we call that
variable an instance variable.
Right.
But now, if you do print hotel one dot watermark.
And we do the same.
Let me duplicate this with control D or command D.
So let's do hotel to that watermark.
You'll see that, of course, we'll get the same value for both of these print functions.
So that makes Watermark a class variable because the value of the variable is shared among all instances.
Plus you can also access that's variable using hotel dot watermark.
Execute and you will see that we get three lines printed out.
So hotel is the name of the class.
So Watermark belongs to that class.
If we do print hotel dot name, you're going to get an error.
So here we go.
Hotel has no attribute name because name is an attribute of the instances.
Now, what's what's the benefit of having a class variable, and when should you have a class variable?
Well, the benefit is that the value of that variable can be shared across all the instances as you
saw.
So that means.
If you think that all the instances should have access to one same value as is the case here.
So all these hotels are part of this real estate company.
So the company perhaps they want to print out that watermark, that label somewhere in the tickets or
anywhere.
So if you have an HTML frontend, for example, you have a web app and you want to display that label
on the web app, then you could get that label from the hotel class like we did here or from the hotel
instance, depending on the scenario.
So that's the difference between instance variables and class variables.
So self is a variable.
In the case of Hotel one, it holds the hotel one instance in the case of hotel two, self holds the
hotel to instance.
And then you get name out of self or watermark.
So self has access to both the watermark and hotel ID and name and so on.
Yeah, I hope that's clear.
And in the next video we'll cover instance methods versus class methods.
I'll see you there.