Object oriented programming allows us to create objects that have their own methods, like run.
And attributes.
Properties.
It's a great way to add more functionality to a language and mimic the real world.
For example, we use the idea of working for a gaming company.
Although Python only gave us a few data types, I was able to create a player character and now my boss
is happy because this player character can be used all over the game and all other coders have to do
is run this line with the custom name and age of the character.
So OP allows us to write code that is repeatable, well organized and also memory efficient.
Right?
Because we only write this once.
So if you want to be a great developer, OP is something that you need to know and you need to understand
because well, this is how you want to organize your code.
Thinking less procedural and thinking more in terms of functionality.
Grouping data like attributes together with methods to create this class that is able to mimic something
from the real world.
Now I want to show you a useful tool here.
We saw that when I do Player Dot.
My editor shows me what I have available the properties as well as the purple boxes, which are the
methods that I have available.
And right off the bat you see that I have a name and run, but then I have all these other ones that
I got by default.
Now these are all Dunder or magic methods, which we're going to get into.
But the key here is that if I do something like help, which is a new function that you may have not
seen before, and let's comment this out.
If I click Run.
I actually get well, the entire blueprint of the object.
If I do something like a list.
And I click Run.
Again, it shows the blueprint that it has.
And this is a great way to see what class blueprint some of the Python data types have.
Now, this is something we'll revisit later.
But I want to go back to this this player character.
Now, in here, we saw that we're able to create attributes.
And attributes are pieces of data that are dynamic.
That is, when we instantiate an object, they are going to be unique to that specific object like name
and age.
And we had to use this self keyword to make sure that it was dynamic.
However, there's another thing called the class object attribute, and it might look like this.
Let's say we had the player character and they had to, let's say, be a paying member.
In order to play this game, you have to have played or you have to have gotten a membership.
So let's have a membership equals.
True.
Attribute.
Now, unlike this one, you see that it's on the same line as our methods.
And this is called a class object.
Attribute.
In a class object attribute, unlike these regular class attributes, is different because, well,
it's not dynamic, it's static.
So if I click.
Run here and we want to make sure that we print something.
So let's print player to.
And I click Run.
While I get the player, that's great.
But if I do dot membership.
And I click Run.
Look at that.
I get that equal to two if I do player one here.
As well.
And I click Run.
I get true again.
So all the players have membership set to true.
But this is not dynamic.
It's a class object attribute, which means it's an actual attribute on this class.
And this is something we use when there's no change.
This is going to be true and exist for all objects, so you can't really modify it.
It's just all the objects that we instantiate will have access to it.
So this doesn't change across instances.
And we can use this down here as well or anywhere in this class blueprint.
As a matter of fact, for example, let's say we want to check if.
Membership exists, so we're checking if membership is true.
Which I mean, it always is.
So this is a little bit redundant, but in here.
I can say only if membership is true.
Then I'm going to assign name and age.
But in order for us to access membership, I'll have to do self dot membership because remember, self
refers to this player character so that if I run this.
Everything still works.
And if I do player one dot name.
This still works.
Or I can refer to this by the actual player character.
So player character membership.
So if I click run here.
This also works because it's a class object attribute.
If I.
Where to?
Perhaps in the run.
Let's change this.
To shout and.
And shout.
It's going to want to run.
Let's remove this line.
It's going to print.
An F string that says My name is.
And give it a name.
Do you think this will work?
Let's give it a try.
Let's say shout.
And make sure we run that method and then click shout again.
If I click Run.
I get an air name is not defined on line 11.
If I go to line 11 here, name is not defined.
Why is that?
Well, because we haven't used self.
So let's do self dot name.
And if I click run.
There you go.
My name is Tom.
My name is Cindy.
Okay.
Awesome.
And I'm able to use self because I pass it into here as the first parameter and all methods receive
the first parameter a self so that we can use them.
But could I just do player character dot name if I click run.
I get an air play, a character has no attribute name.
Now, why is that?
Because name is not a class object attribute.
It's not actually a property or an attribute of player character.
No, it's defined in our constructor function, our init function.
So that's a bit of a gotcha here and something that you'll have to get used to.
A class object attribute is something that doesn't change across different instances versus an attribute,
or a class attribute is something that is dynamic and specific to each class object.
The way we access them is different where we can actually access membership like this.
But in order for us to access a name or age anywhere inside of our class, we have to use the self name.
Which is why any time we create a method in a class, even if I create a new one over here, and let's
say this new one is going to be run.
Well once again I have to use self as a parameter and then anything else that I want to add.
And here.
As the second parameter so that when I click, let's say run.
I pass in the.
Hello.
Argument like this.
I know this is a lot, but hopefully you're starting to follow the rules of classes.
Once you remember these rules, then it becomes easier and easier.
But we'll practice this a little bit more in the next video.