Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Class and Instance Attributes

This lesson is part of a Real Python video course by Austin Cepalia.

00:01 Welcome back to object-oriented programming in Python. In the last video, I asked the question, “Where might we use classes and objects in real software?” I came up with two examples: video games and web browsers. For a video game, we might define an Enemy class that holds properties like the name of the enemy, their current health, and their power level. For behaviors, they might be able to attack, take damage, and finally—if their health is low enough—declare defeat. Video games are a great example of complex object-oriented software design. They are built with lots and lots of classes, and they all rely on one another in some way or another.

00:44 The web browser you’re using right now could also be designed with OOP. Individual tabs could each be their own objects, instantiated from a Tab class. The properties could be the title text, whether or not this tab is currently the one being viewed, and also the web page the tab is showing. For behaviors, we could have close and reload.

01:06 Now, look at each of these properties carefully. I think the title text is best represented as a string, and whether or not the tab is open? Well, that should be a Boolean, True or False.

01:17 But what type should our web page property be? There’s no data type built into Python that can store everything about a web page. So, here we can actually create a separate class called Page with its own properties and behaviors.

01:33 As you can see, the properties of a Python class aren’t limited to the built-in data types in Python, like int and str. Our own custom types, like Page, can also be used as properties of other classes.

01:46 When we instantiate the Tab class now, we’ll have to provide it with a Page object to store, just like we have to provide it with a str object for the name.

01:56 The point of this exercise was to show you how to think of software as a collection of objects that interact with one another. In my experience, the hardest part of learning OOP is not actually learning the concepts themselves, but learning how to design software in terms of OOP.

02:13 Planning out your software project often takes a lot longer than actually coding it. It’s something that can challenge both new and seasoned developers.

02:23 As you know by now, a class defines both the properties and the behaviors for its objects. So now, let’s actually write one. To define a new class, we start with the keyword class followed by the name of the class.

02:37 Easy enough. And, well, we haven’t really learned how to define properties or behaviors in Python, so let’s just write pass. This special keyword tells Python that this is an empty class.

02:50 Before we can add properties to our class, we have to learn about how they work in Python. First off, properties are actually called attributes in Python.

02:59 There are two types of attributes: instance attributes and class attributes. Instance attributes are what we’ve seen before. These are the attributes that are independent to each object, like the door color or the height, in the previous example. In this example, our Dog class has two instance attributes: .name and .age.

03:21 This def __init__() is a very special function that belongs to our class. This is called the initializer and it’s sort of similar to a constructor—if you’ve ever seen that in another language.

03:35 We don’t actually call this function ourself. Instead, Python will automatically call this function when we instantiate the class. In other words, this function is used to construct a new object from the class.

03:49 The job of the initializer is to provide our object with its initial attribute values. You can see this initializer takes three parameters: self, name, and age. When we instantiate the Dog class, we’ll need to pass in values for name and age, and then this function will assign the new object those values. self is a very special keyword, and the fact that we have to include it in our parameters is one of those little Python quirks.

04:17 We don’t pass a value in for self when we create the object. Instead, self is used to refer to the current object being created during instantiation.

04:26 So when we say self.age = age, we’re saying, “Take the value of age we pass in, and assign that to the new object’s .age attribute”—and the same goes for name.

04:40 Remember before how I said that each object has its own independent values for its attributes, like how one door can be red and the other one gray? Well, that doesn’t actually have to be the case.

04:52 This is where class attributes come into play. Unlike instance attributes, which are independent for each instance of the class, class attributes are the same for every instance. Think of it like this: with instance attributes, we could paint one door a new color, and it wouldn’t affect the colors of the other doors. However, if we make the color a class attribute and we set it to yellow, then all doors would be yellow by default when they’re instantiated.

05:21 This is just a default value for the class. This doesn’t mean that after we instantiate all of our doors, changing the color of one door will change all of them.

05:31 If you’re thinking static right now, this is a little bit different. In the example of the Dog class, .species is defined outside of the initializer function, which means that it’s a class attribute.

05:43 This means that every Dog we instantiate will by default be a mammal, and we can’t change that when we create a new Dog object. In the next video, we’ll start programming our Dog class and we’ll see how class instantiation works in Python.

You must own this product to join the conversation.