Class attributes are just a really fancy way of saying it's
a variable inside of a class.
Why we don't just call them variables?
Well, it's because a variable can live outside of a class,
and a variable inside of a class is usually scoped to it.
So it has a special name because it has a special purpose.
So we call them attributes.
Now, they're typically used with the 'self' keyword, which
we saw in the Introduction to Object Oriented Programming
video. But we're going to dive into that a little bit more in this video.
So whenever you're working inside of a class, and you see
'self.', some variable name, as soon as you see 'self.', you
can just assume that that is an attribute, not a variable.
It's a variable type.
It's a subset of a variable called an attribute.
So let's go ahead and create our first class. So 'class MyClass',
and in here we have this magical function called '__init__'.
So we're going to create just one method here, one function,
and it starts with '__init__',
and it takes 'self' by default.
Now, anytime you see '__', or a double '_',
or as we call it in Python, a dunder,
anytime you see a dunder, you can assume that that is built into Python.
And so this is going to have some sort of magic power behind
it, something that we don't yet, just out of the box, something
that we get to work with that Python gives us, but we should
not try to change it by any means.
We should work with it.
So in here, I'm just going to say 'pass' for now, and what
I want to do, is just add a variable.
So let's run this for now, and we've got 'my_class =
MyClass', and instantiate it with parentheses.
But 'my_class' currently has no attributes.
It doesn't have a name, it doesn't have a purpose, it doesn't
do anything.
So let's add our first parameter in here.
So 'param1' is going to be anything we want.
And I'm just going to keep this simple and very generic for the time being.
So we could 'print(param1)', and let's run that, and run
that, and we're going to see that we're missing a positional
argument, just like a function.
If a function is looking for an argument and it doesn't get
it, you're going to see a very, very similar error.
So let's just call this, "The first param", and run that,
and it prints out the first parameter.
Now, if I want that attribute, if I want to be able to use
it from my variable here, I would like to use 'my_class.
param1', and this is not going to work.
And the reason for that is because 'my_class' doesn't have
an attribute called 'param1'.
It does inside of '__init__', but it does not inside of the entire
class. Again, that comes down to variable scoping.
'param1' doesn't exist anywhere.
So what we do to make it exist in this entire class, and not
just in here, but also in here, we can say 'self.param1 = param1'.
Now, this looks a little weird because we're basically assigning
the variable to itself,
but that's just something we need to do in order to get access
to that parameter through our variable.
So if we rerun this, that, and that, it will now say, "The first
param". Alternatively, you can add things by default.
So in here, just underneath your class, you could type 'course
= "Python for Everybody"',
and we do not need to run
'self.course = course', because that's already in here.
We've already defined the attribute.
And so if we run this, this, this, and then do 'my_class.course', it
prints out the 'course'.
Now, you might be thinking, "Well, how come I have to use
'self' here, but I don't have to use 'self' here".
That's a good question,
and we're going to see this a little bit later.
But if you have another function in here,
[no audio]
what 'self' does is, 'self' basically says, "Hey, 'param1'".
It's essentially like saying 'param1 =', default
value. And so whenever you see 'self.', and then the variable
name or the attribute name, what it's actually saying is,
"Hey, jump outside of this function here.
Just jump just outside of it,
not outside of the object, but just jump outside of the function,
grab that variable name, and assign it a new value".
And so in other methods and other functions, we can use
'self.param1', and we can use 'self.course'.
Now, this gets a little beyond the scope of this particular
lesson, but what I would like you to do just to get some
hands on experience here, is I want you to create a new class,
could be named anything,
remember, camel casing, assign a regular parameter here just
inside of the class, but not inside of a method, not inside of a function,
then create the magic '__init__' method.
Remember, two '_'.
Every function inside of a class always starts with 'self',
and then your first parameter, assign 'self.param1'
to whatever that parameter is going to be.
Instantiate your class, and then use whatever your variable name is,
in my case, it's 'my_class.param1', and print that out,
and then print out 'my_class.course'.
So go ahead, write that out by hand.
It will give you a little extra muscle memory
when it comes to understanding how all of this works.
This whole thing should only take you about 2 or 3 minutes.
Once you've done that, let's head on over to that next Object Oriented
Programming lesson.