Welcome back.
In the previous video, we saw that we did some big boo boo.
That is, we over wrote name and speak.
And now all that hard work that we put into our class has been overridden by this.
I mean, sure, if we instantiate a new object.
We will have our run and speak, but player one now is completely useless.
It has no name and speak.
It just says Boo.
Completely useless.
Now in Python, there's this idea of public and private, and this is related to our discussion of abstraction.
The idea behind abstraction is that we hide away information and only give access to things that a user
is concerned about.
Right.
So ideally, we shouldn't have access to init.
Ideally, we shouldn't be able to, let's say modify, run or modify name and h, because when we create
a player character in our game, hopefully the player can just randomly change their name or their age.
And sneak into our game that perhaps is only 18 and over.
So can we make a private variable in our class?
Well, here's the thing.
Some languages allow us to have private variables, for example, in a language like Java.
I can actually say that an attribute is private like this and I won't be able to access it and modify
it like I have this way.
But in Python, there's no true privacy, no true private variables.
So how do we get around this?
Well, what we would do is do underscore name or underscore H.
Now.
Does this give any special power?
No, this is just a convention that is as programmers, as Python programmers, we determined that,
hey, if we see underscore in our code, that most likely means that this should be a private variable.
So that when I use it and speak, let's say underscore name, underscore H.
And let's comment this out.
If I click Run.
Well, I have to run this because now it's a method.
Let's click Run.
And I get my name is Andre and I'm 100 years old.
It all works.
But if I try to overwrite these and click Run.
Oh, and now because it's no longer a method, let's remove speak and I click run.
This still works.
And that's because, like I said, there's no true private variables.
As programmers, we've decided that underscore means that you shouldn't touch this.
Please don't touch this.
You can still be bad and do something like this.
But I'm letting you know ahead of time if you c underscore this shouldn't be modified, this should
be private.
And that's how we achieve privacy in Python.
We all kind of agreed to say, hey, let's keep this little thing private.
So if you ever want to keep a method or a an attribute private, you just put an underscore in front
of it, but it's no guarantee.
What about this double underscore init?
Again, this is something we're going to be speaking about very shortly, but it's called a dunder method.
That is, it's built into Python.
And we usually never write our own Dunder methods.
We would never write double underscore.
Write this.
These double underscores and there's lots of them have special meaning in Python.
And the reason that they're named like this is because they're saying, hey, if you're writing double
underscore for your variable, you're doing something very wrong.
You're about to overwrite something.
So once again, this double underscore is also a convention to let people know you shouldn't really
touch this or modify this with each data type.
Let's say a list.
If I do dot, you'll see that as you scroll down.
You have a lot of these dunder methods.
Again, something that we're going to talk about, but these are here to let you know.
Don't overwrite these if you're doing something like underscore.
Underscore, I don't know, doc, like this.
That's bad.
Don't don't try and modify it and assign it to five.
So this idea of a private field is important to Python.
And although we can overwrite a lot of things, it's bad practice.
But let's bring it back to abstraction.
The idea is to abstract away this code, and although it can be modified and overridden by using these
proper conventions like private attributes, we're able to abstract things away, but still make sure
that whatever the user might be using isn't going to break our code.
I'll see you in the next one.
Bye bye.