Class and Instance Attributes
In this video, you’ll learn where classes and objects are used in real software, as well as how they’re defined in Python.
We can define an empty Dog class like this:
class Dog: pass
Classes contain characteristics called Attributes. We make a distinction between instance attributes and class attributes.
Instance Attributes are unique to each object, (an instance is another name for an object). Here, any
Dog object we create will be able to store its name and age. We can change either attribute of either dog, without affecting any other dog objects we’ve created.
class Dog: def __init__(self, name, age): self.name = name self.age = age
__init__ is called the initializer. It is automatically called when we instantiate the class. It’s job is to make sure the class has any attributes it needs. It’s sometimes also used to make sure that the object is in a valid state when it’s instantiated, like making sure the user didn’t enter a negative age for the dog.
We have to include the
self parameter so that our initializer has a reference to the new object being instantiated.
Class Attributes are unique to each class. Each instance of the class will have this attribute. It’s sometimes used to specify a defualt value that all objects should have after they’ve been instantiated. Here, our class attribute is species
class Dog: species = 'mammal' def __init__(self, name, age): self.name = name self.age = age
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.
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.
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.
As you can see, the properties of a Python class aren’t limited to the built-in data types in Python, like
str. Our own custom types, like
Page, can also be used as properties of other classes.
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.
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.
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:
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.
The job of the initializer is to provide our object with its initial attribute values. You can see this initializer takes three parameters:
age. When we instantiate the
Dog class, we’ll need to pass in values for
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: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.
If you’re thinking static right now, this is a little bit different. In the example of the
.species is defined outside of the initializer function, which means that it’s a class attribute.
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.
Become a Member to join the conversation.