Now the best way to learn is to actually see code in action.
So in theory, let's actually code our own class.
I'm going to just delete everything and start from scratch.
And let's say that we're working for a gaming company, and this gaming company has, well, this wizard
game that they want to create, almost like Harry Potter.
Now, how can we think about it, an object oriented programming?
Well, the first thing that we're going to need is we're going to have to have players.
Right.
Each player has a character that they need to play.
So let's create a class here and call it player.
Character.
Again.
Notice the naming convention because it's a class.
And.
Another rule is that usually you want it to be singular, not plural.
So you can't say player characters because I mean, it's a blueprint.
From this blueprint, we're going to create many characters, but those are going to be objects.
So player character, single in singular.
Now in here, I'm going to do a few things and I'm going to warn you, this is going to look confusing
at first, but bear with me.
I will explain it.
We're going to define a underscore, underscore in it.
Underscore, underscore.
Now in here, we're going to say self.
And then give it a name.
The name of the character.
From here, we're going to do Colin once again.
And simply say self dot name.
Equals to name.
Oh, boy.
What just happened here?
Again, I'm going to hold off explaining what this does and just write a few more lines of code so that
it makes a little bit more sense.
I'm going to write another method in here and I'm going to define, let's say, a run method that each
player can run.
And in here I'm going to once again have this weird self ward and then simply inside of here, say print.
Run.
All right, so I've just created this class.
It's still kind of confusing.
We don't really know what this in it is, what the self is, and this whole new look.
But let's ignore that for a second and simply say.
Player.
One is going to equal player character and instantiate that player.
So that.
We can print player and see what's up with this player.
If I click Run.
All right, I get an air.
It's a type air.
It says it missing one required positional argument name.
All right.
Let's take a break here and see what is happening here.
You see this, innit?
Method is a special method.
You see the two underlines here.
This is called a dunder method or a magic method.
And we're going to have a video specifically about this.
But when we're building a class, you usually see this at the top.
And this is what's often called a constructor method or an init method.
And this is automatically called any time we instantiate.
Remember Instantiate means is we're calling the class to create an object.
So when I do this, it's going to automatically run whatever is in this code block.
So when I run this, you see that I'm getting the error online nine right here, online.
Nine.
And when I try to instantiate the player character.
It runs in it and then it tries to do self.
Name equals to name.
But the problem is that I'm not giving it an argument name.
And that is because when we call in it, we accept whatever is after self as the parameter.
Name.
So we need to give it a name of this character.
So let's say the name is Cindy.
If I click Run here.
All right.
It looks like I have a player character.
And as a matter of fact, it even shows me that it's an object at this memory location.
So on our computers, this is where the player character player one is located now.
Very cool.
But what is this self keyword and what is this code online for?
This is a way for us to define.
Well, self self refers to the player character.
It's saying, hey, this is going to refer to this player character that we're going to create player
one and I want self dot name to equal whatever the parameter name is.
So that if I do player dot name here and I click run.
I get Cindy.
And why is it Cindy?
Well, because I gave it the parameter player character.
And when I instantiated it, I passed the name parameter.
The default parameter is self when I do in it and in classes, you see here that the default always
the first parameter when we're defining a method is self.
And now I give name to self dot name.
What if I remove this here and I click run?
You see that it says object has no attribute name.
Because in order for me to say, Hey, I want player to have a name, I need to say self dot.
Because self refers to player one.
So if you remember when we had a list, we had to do dot and then we would say append.
Well, somebody wrote this append code and in the append code it's going to say self dot.
Let's say add to the list and self refers to whatever's to the left of the dot.
So this way it allows us to have a reference to something that hasn't been created yet in this case.
Player one And let them know that, hey, when it's created, when we instantiate, then I want you
to make sure that player one has this name attribute so that if I create another player and player Tom
is going to be player two.
If I print player to name and I click run.
I have two different players with two different names.
I was able to use the same piece of code but have different values because the self, every time I instantiate
this refers to first time player one, then first second time player two.
Now self is one of those things that gets really, really tricky to understand.
And you might need to watch this video a couple of times, but the key here should be evident to you.
That is, I'm able to write code only once.
Remember our principle drive?
Do not repeat yourself.
And I'm able to make it dynamic by saying this data that we're going to create for the object is going
to be dynamic and it's going to change based on what we give it.
So that I can create different players with different attributes.
But still use the same code.
Now I can have multiple things here.
I can have H for example, and once again I can say self h equals H, and then Cindy will have to pass,
let's say 44 and Thomas 21.
So that here once again, if I do age.
They'll have different attributes.
So remember, these are attributes or properties that these objects have, and you can access them by
doing the dot notation and then accessing the property itself.
You see that I don't have any brackets here because it's not a method, it's not taking action.
It's simply saying, Hey, I want to access this attribute.
If I do dot here, my editor will actually tell me and we'll have the symbol for, hey, you have access
to the attribute, age and name.
And as a matter of fact, I also have access to run the method so that if I click Run.
Well, remember, a run is a method.
So I get this bound method, but it's not printing anything, it's not taking the action.
So we have to once again add the brackets.
Hit, run.
And look at that.
I'm able to do run.
Then I get none.
Then 21.
Why do we get this?
None?
Because remember, this is a function that doesn't return anything.
When a function doesn't return anything, what do we get?
We get none.
So maybe I can just say return done.
If I click Run.
There you go.
We have run done 21.
Now, one last thing before I end this video.
The interesting thing here is if I do player one.
And then player two and I print this and I click Run.
You'll see that I get the player character.
But the object each of these objects, player one and player two are at different memory location.
So I'm able to use one piece of code, one blueprint to create multiple players.
But these players are not the same.
They're created from the same blueprint, but they live in different places in memory.
And that makes sense, right?
We don't want Tom and Cindy to be the same player.
They're two different players.
So this way we're able to keep things safe.
And anything I add to Player two, for example, let's say Player two can now attack.
And attack will just be a property that will be 50.
Now, if I do not attack here, you see that player two has.
Attack.
But if I do play or want attack.
I'll get an air.
Object has no attribute.
Attack.
I hope you're starting to see the power of classes.
Although it may be difficult to understand at first.
The power becomes evident when we are able to create this blueprint that we can now use over and over
and over to create our own objects.
Let's explore more in the next video.