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 class attributes to your Car class to start building out the blueprint for car objects.
- Attributes = the name in Python for the properties of an object
- Class Attributes = attributes that are the same for every instance created and are created on the class
- id() Documentation
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
In this video we're talking about
the properties of an object.
0:00
In Python these properties have
a particular name, attributes.
0:05
There are two kinds of attributes
we can add to our car class,
0:10
class attributes and instance attributes.
0:15
Let's start with class attributes.
0:18
Pop over into workspaces and
let's get coding.
0:21
It's finally time to remove the pass
keyword and start building out our class.
0:25
Class attributes will be the same for
every instance so
0:31
let's think of some
attributes all cars have.
0:35
All cars have four wheels, At
0:42
least two doors, And an engine.
0:47
These attributes are added to
the class like regular variables.
0:56
Great, now let's make sure we
can access these attributes.
1:00
Let's create two variables that each
hold an instance of our car class.
1:05
I'm going to call the first one, car_one.
1:11
And the second one,
you guessed it, car_two.
1:16
To access the attributes we
need to use dot notation.
1:22
Letโs print out the doors attribute for
both of our instances.
1:26
Car_one.doors and
1:35
car_two.doors.
1:40
When we run the file and
look in the console,
1:45
Weโll see 2 printed twice.
1:51
Since these are class attributes
we can also access these
1:53
attributes without creating an instance.
1:58
We can use the dot notation
directly on the class itself.
2:02
Print Car, with a capital C, dot doors.
2:09
When the file is run 2 is
now printed out three times.
2:17
But what if a car has four doors
instead of two, how do you change it?
2:24
Let's change car_oneโs
doors attribute to four.
2:29
All we need to do is set car_one.doors
equal to the number four.
2:34
Iโm gonna copy and paste this below.
2:45
Let's rerun the file.
2:53
And we can see that only car oneโs
doors attribute has changed.
2:58
If you were to set the doors
attribute on the class to four,
3:05
It changes all of the doors
attributes since our
3:19
instances are built from our car class.
3:23
What happens if you do both?
3:27
What if you change an instances
doors attribute to say six and
3:30
the classes doors attribute to three,
what sticks?
3:35
Go ahead and pause me and
try it out on your own,
3:40
this is a great way to learn
more about how things work.
3:43
See you in a bit.
3:47
Welcome back, did you test out your code?
3:50
I'll show you what I did.
3:54
First, I changed the print
statements into f-strings, which is
3:56
the newer way to format strings, this
will help make our printouts more clear.
4:01
Then I made the attribute changes with the
instance change before the class change.
4:07
When I run the file,
4:15
car_one's doors attribute is now 6 and
4:24
the rest are now 3.
4:29
If I move car_one's attribute
change to after we change
4:32
the car classes attribute and
run the file,
4:36
I get the same result.
4:51
Changing the attribute on the class will
change the attribute on the instances,
4:54
unless you have changed
the attribute on an instance itself.
5:01
Let's try changing the doors
attribute on the class again and
5:07
print out the doors one more time.
5:10
Yep, car_oneโs doors are still 6 and
the rest are now 5.
5:21
Why does that happen, you ask?
5:28
Well, Python does this cool thing where
it saves memory by using one location for
5:30
more than one attribute.
5:36
I know this is getting super technical,
but it does help explain what's going on.
5:38
We're going to use ID,
5:42
which returns a number representing
the address of an object in memory.
5:44
Print ID car_one.doors.
5:55
And I'm gonna copy it just to
make it a little bit faster.
6:02
Capital C, Car.
6:18
Let's comment out our door changes.
6:23
And run the file again.
6:29
You can see all of
the numbers are the same,
6:35
theyโre at the same location in memory.
6:40
Now, let's uncomment our changes.
6:45
And I'm gonna copy our prints here,
And paste them.
6:50
Now let's run one more time.
7:01
And you can see the numbers have changed.
7:09
car_one no longer shares the same location
and memory as the classes attribute.
7:15
This is why car_one's attribute
doesn't change when we
7:22
change the attribute on the class itself.
7:26
Tricky little things like
this can happen in your code,
7:31
which is why it's important to
play around and test things out.
7:35
The more you get used to playing
around in Python the more you'll learn
7:39
about how things work.
7:44
Take a minute to practice
before moving on,
7:45
try creating your own class
with a set of class attributes.
7:48
Then create a few instances and
7:52
try changing your attributes on the class
itself and then on your instances.
7:55
Print out your attributes and
see what's happening behind the scenes.
8:01
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