Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Add instance attributes to further customize each object created with your class.
- Attributes = the name in Python for the properties of an object
- Instance attributes = attributes that are created when an object is instantiated
- Method = a function inside of a class
- Dunder method = a nickname for a method that has double underscores before and after its name (Ex:
__init__
)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's look at instance attributes.
0:00
Remember an instance is
when we instantiate or
0:03
create an object of our class.
0:07
Instance attributes will also
be created when we instantiate
0:10
an instance of our class.
0:14
We do this by adding
an initializer to our class.
0:17
The initializer is a function or
method as it's called in a Python class.
0:21
It's placed inside of our car class.
0:27
This dunder or double underscore
method structures each instance.
0:41
You've probably noticed this method
takes an argument called self.
0:48
Self is a reference to
the instance when it's created.
0:53
I know this is getting a bit confusing,
but stick with me.
0:58
Let's set our instance attributes to
help explain what's going on here.
1:04
From our car example, make model and
year are typically unique to each car.
1:09
And so we need to be able to give
each car or instance specific values.
1:15
We can do this by adding make model and
year as arguments to the init method.
1:20
In order to set these attributes,
1:30
we need to use self plus
the name we want to use for
1:32
instance attribute and set it equal
to the value we are passing in.
1:36
If we run the file now
1:51
We get an error.
2:01
It says we're missing three
required positional arguments,
2:03
make model and engineer.
2:06
These are our three instance attributes,
you can see self is missing.
2:08
And that's because we don't need to worry
about passing it in, that's done for us.
2:15
But we do need to pass in
the make, model and year for
2:21
each instance that's instantiated.
2:26
For our car_one instance,
2:30
let's pass in Ford, Model T and 1908.
2:32
For car_two, let's do my dream car,
the Rolls Royce Phantom.
2:44
Hey, a girl can dream right?
2:56
When we run the file again.
3:00
No errors.
3:06
Now let's access our new attributes.
3:09
Instance attributes are accessed
the same way with dot notation.
3:13
Let's try to access the make
using the class too.
3:18
Run the file.
3:40
And error. Our two instances
have their make printed out,
3:44
so that worked, but
we get an attribute error on the class.
3:50
The car class doesn't have
an attribute named make
3:55
That's because our make attribute
is an instance attribute.
3:59
So it can only be accessed
with an instance.
4:04
Never be afraid to try things out and
see what happens.
4:09
The more you look at error messages,
4:13
the easier it'll be to
understand what they mean.
4:16
Let's remove that line for now.
4:19
Changing an attribute is as easy as
setting it equal to something new.
4:27
Let's change car_one's year
attribute to something else.
4:33
Run the file.
4:58
And cool, the console shows
car_one's year is now 2015.
5:01
Now, what if we needed to create
a bunch of cars all with the same make?
5:05
Do you still have to pass
in the make each time?
5:12
Luckily, Python allows
you to create a default.
5:17
We'll need to do a bit of
rearranging since it's best
5:21
to put the default arguments at the end.
5:24
Let's make the default Ford.
5:34
Now we can remove it from
car_one.
5:43
To match the order, we also need to
move car_two's make to the end,
5:48
otherwise the make would be 2020.
5:55
Run the file again.
6:06
And great, it still works.
6:10
All right, I threw a ton at you.
6:13
So let's do a quick recap and
6:15
I'll leave you with a small
challenge to practice your skills.
6:17
If you need a quick mental break before
the recap, feel free to pause me stretch,
6:21
grab some water, whatever you need.
6:26
I'll be right here when you get back.
6:29
Recap time,
instance attributes are created with
6:33
an initializer inside of the class.
6:37
The initializer is a method called Dunder
init where you pass in the attributes
6:43
and then set them using self plus the name
of the attribute is equal to the argument.
6:52
You can set a default value on
an argument if you need it.
7:00
You can access instance attributes
using dot notation, but
7:05
only on an instance and not on a class.
7:10
You can also change attribute values by
setting them equal to something else.
7:14
I think that covers it.
7:20
Now for the challenge,
try creating a class of your own, give it
7:21
an init method and
at least three instance attributes.
7:26
Make one attribute have a default
value and then create a few instances,
7:30
print things out and play around.
7:35
See you next time.
7:38
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up