Welcome back.
What I haven't told you is that when learning about object oriented programming again and not just in
Python, but in any programming language that supports OP paradigm, is this idea of four pillars of
OP the four things that object oriented programming does really, really well.
And as a matter of fact, we've actually learned the first one, and that is the idea of encapsulation.
What is encapsulation?
Encapsulation is the binding of data and functions that manipulate that data.
And we encapsulate into one big object so that we keep everything in this box that users or code or
other machines can interact with.
And this data and functions are what we call attributes and methods, right?
We're able to encapsulate the functionality of our player character by having name and age data or attributes
and also have functions that can act upon this name and age.
So, for example, I can have a method.
In here that says Shout.
Or speak.
And the speake method can now use the name and age.
I can say.
My name is Self Dodd name.
And I am.
Self.
Eight years old.
So that if I do player one dot speak.
And I run this.
My name is Andre, and I am 100 years old.
I'm encapsulating the functionality.
If I didn't have this class, I would have, well, some variables and then two functions.
But by using encapsulation, I've packaged this up into a blueprint that I can create multiple objects
of, so that when other people see my code, they know, Hey, this is an entire object that I can interact
with.
I can use in certain ways that player character describes.
And we've also seen this in our built in Python data types, right?
When I create a string.
Like this.
Because of encapsulation, I have all this functionality available to me, all these methods that I
can access so that if I do, let's say capitalize, it will capitalize all my strengths.
I have all this functionality encapsulated for me to use.
Now.
Why do we want to package data and functions into attributes and methods?
Well, because this gives us extra power, right?
If, for example, this player character doesn't have any actions, any methods and just had attributes.
Well, in that case, this is kind of like a dictionary, right?
It's kind of useless if I print player one here and I click run.
I have this player object but I can access name like this.
And I can also access H.
Like this.
But I can't really do anything to it.
I could have the same dictionary here.
Let's say player two that is going to equal a dictionary that has name of Andre and then age of 100.
And the only real difference between the two is the way I access this information.
Instead of the dot notation, I use the bracket notation to access the keys.
So that when I click Run.
Oh.
I have to make sure that I do two here for player two.
If I click Run.
Again.
It's the same thing.
Sure I can do it this way, but it's kind of useless.
Why not just create a dictionary if all I want is data?
But using this way, I'm combining things, packaging things up into those instances, into those boxes
that can be useful and also things that have meaning.
Right?
Because our world is full of data and actions, right?
Even a tree, for example, if we had a tree class in our real world, the tree would have maybe whether
it was an evergreen or a perennial, it will have whether it has lease or it doesn't, how tall it is.
We'll have all that information, but also actions.
Right?
I can cut down a tree.
The tree can grow, the tree can extend its roots.
So this way we're able to mimic what happens in the real world with our code.
So that is encapsulation and we've seen it.
But let's talk about our second pillar in the next video.