Hey, welcome back.
So far, you learned about instance variables and instance methods.
And these two can also be categorized in the instance attributes category.
So these are attributes of an instance.
So instance variables and instance methods.
Then you learned about class variables and class methods.
These two are also known as class attributes.
Right.
So we have instance attributes and class attributes.
So attributes can be either variables or methods, right?
Now, in this video you will learn about properties, actually instance properties.
So a property is a method which behaves like a variable.
You'll see how.
Let's go down to the reservation ticket class.
We have two instance methods here currently in it and generate.
And we will add another method which is going to be a property.
So a special instance method called a property.
So let's say that maybe the customer, they enter their name in lowercase letters and maybe they enter
some unnecessary spaces when they type in their name.
And so we want to clean that out, to remove the spaces and capitalize the first and the last name of
the customer.
A good way to do that would be inside a property and I'll explain you why.
So let's create a.
A method is called the customer name.
You see, this is not a verb because this is a property.
It makes more sense to be more like a variable name.
I'll explain why when you call that property.
And then you see a local variable.
So this is just a variable which has meaning inside this particular method.
This will be equal to self, that customer name that strip.
So the strip method removes any spaces.
So let's say that the customer name is John Smith with some space at the end.
What we're doing here is customer name that strip and that will return.
John Smith without the space.
Right.
And then again, we say name is equal to name.
Dot title.
So name will be John Smith without space and.
Basically the second line we're doing this, so we get John Smith cleaned out with Capsulated in J and
S, and then we return name.
Now this looks just like a normal instance method to make this an actual property you use property decorator.
I know you're still confused.
What's the point here?
Well, you'll see.
What's the point there?
So let's create a ticket is equal to reservation ticket.
That's the name of the class which has this property.
Right.
Reservation ticket.
It needs a customer name and hotel object.
Customer name.
Let's say the customer entered the name John Smith with the space and with lowercase letters.
And hotel.
Object.
Is equal to a hotel instance.
Hotel one, for example.
Now with ticket dot the customer name.
You can access the returned value of the customer name.
Let's print this out.
Print that and see what we get as output.
So we get John Smith with Capital J and Capital SE.
Now that means we can use the customer name.
Insights here as well to generate the tickets or self the customer name, right.
And so the benefit of this is that you don't need to call this.
That's what makes this a property right.
It behaves like a variable.
It's defined as a method, but this decorator makes it behave like a variable.
So let's call the generate method.
Tickets generate generate doesn't take any.
Arguments other than self.
So we should see the output with John Smith printed out on the ticket.
And so that's about properties.
So you should use a property.
When you think that the value this method returns looks more like an instance variable, but the value
needs some processing.
So you could also place these lines in the init function.
So to modify the customer name by stripping it out and applying a title method, but that would be too
much for the init function for the init method.
So it makes more sense to create a property and then you can just access the customer name.
As if it was a normal instance variable.
So the benefit is that you keep this code enclosed inside a method, so that keeps things more organized.
And yeah, that's about properties.
And we have some more videos, so I'll see you in the next ones.