Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

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:

Python
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.

Python
class Dog:

    def __init__(self, name, age):
        self.name = name
        self.age = age

This __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

Python
class Dog:

    species = 'mammal'

    def __init__(self, name, age):
        self.name = name
        self.age = age

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.

Peter T on March 22, 2019

In the text below the video, don’t you need to change: “Behaviors are actually called Attributes.” to “Properties are actually called Attributes & Behaviors are actually called Methods.”?

Dan Bader RP Team on March 22, 2019

@Peter: Good catch, this should be fixed in the description text now. Thanks :)

mnemonic6502 on Jan. 18, 2020

At this point it would have been better if the learner wasn’t expected to mentally context switch thinking from the hypothetical door class then to a dog class and just stuck to one example class initially and built on that.

TheManWhoSoldTheWorld on June 11, 2020

Nice videos! I would like to know how decide when use class vars and when not.

Thanks.

kiran on July 16, 2020

what is difference between object & instance. give me any one example. EX: class A: pass obj = A()

in above example A is the class name. obj is the object. what about instance?

Austin Cepalia RP Team on July 16, 2020

@manupanduworld “instance” is another word for “object”. To create an instance of a class is to create an object from it.

Ravi Dayabhai on Nov. 19, 2020

‘Mammal’ is a class rather than a species…how apt for these lessons! 🤓

Leonardo Di Domenico on July 30, 2021

And what about encapsulation? What does prevent me from modifying the species variable in order to transform a mammal into a reptile? Am I in advance? :)

Bartosz Zaczyński RP Team on July 30, 2021

@Leonardo Di Domenico In Python, there’s no concept of “private” variables or even constants with final values. There are ways to make it more difficult to change the state of an object, such as using property descriptors, but ultimately, Python will trust you to be a responsible citizen.

Become a Member to join the conversation.