A Python class attribute is a variable that belongs to a class, not an individual instance, and is shared among all instances. An instance attribute is a variable that belongs to exactly one object. An instance attribute is defined inside the __init__()
method by using the reference to the instance self, whereas a class attribute is defined outside the __init__()
method.
As you go through this tutorial, feel free to watch my explainer video:
Today, you are going to learn about the terminology of “attributes” because you need to understand the term and the concepts behind the term to be able to master more advanced stuff.
There are two types of attributes: class attributes and instance attributes.
- Class attributes are variables that are defined for the class itself. Class attributes are shared by all instances—a change will be visible in all instances.
- Instance attributes are variables that are defined for each instance separately. If one instance changes the instance variable, other instance variables are not affected.
Class Attributes
Class attributes are variables that are defined for the class itself. Class attributes are shared by all instances—a change will be visible in all instances.
This can be seen in the following example where both car instances p1 and p2 share the same class attribute brand. If you change it for the class, will both instances see the change?
class Car: brand = 'porsche' # Class attribute # Create two instances p1 = Car() p2 = Car() # Change the class attribute Car.brand = 'tesla' # Check the value of the class attributes of both instances print(p1.brand) print(p2.brand)
The output of the code snippet is:
tesla tesla
So, both instances see the changed class attribute.
Instance Attributes
Instance attributes are variables that are defined for each instance separately. If one instance changes the instance variable, other instance variables are not affected.
The following code shows you that changing an instance attribute of one instance won’t change the instance attribute of another instance.
class Car: def __init__(self): self.brand = 'porsche' # Instance attribute # Create two instances p1 = Car() p2 = Car() # Change the instance attribute p1.brand = 'tesla' # Check the value of both instances' attributes print(p1.brand) print(p2.brand)
Consequently, the output is:
tesla porsche
Let’s make these differences stick in your brain with three quick code puzzles, shall we?
Class vs Instance Attributes Puzzles
Let’s have a look at an example brought up by one of my email subscribers (I have modified it a bit for equality of genders). By the way, sign up for my list to get free Python cheat sheets, code puzzles, Python lessons, and participate in code contests. It’s huge fun — you’ll see! ??
class Partner: database = [] def __init__(self, name, age, likes_me): self.database.append(self) self.name = name self.age = age self.likes_me = likes_me Maria = Partner("Maria", 21, False) Florian = Partner("Florian", 116, False) Eve = Partner("Eve", 22, True) Fiona = Partner("Fiona", 55, True) for partner in Partner.database: if partner.age<25 and partner.likes_me==True: print(partner.name +"(age " + str(partner.age) + ") likes you!")
Puzzle 1: What is the output of the code snippet?
This example shows both types of attributes: the class attribute “database” and the instance attributes “name”, “age”, and “likes_me”.
The class attribute is defined outside of the constructor __init__(…). You simply create and initialize the variable within the class (e.g. “database = []” in the example). From the outside, we access this variable via the class (e.g. Partner.database) not via the instance (e.g. Maria.database).
However, we actually COULD access the class attribute via each instance like this:
print(Fiona.database == Maria.database)
Puzzle 2: What is the output of the code snippet?
This is different than instance attributes:
print(Fiona.age == Maria.age)
Puzzle 3: What is the output of the code snippet?
The answers to the puzzles are given below at the end of this article.
Summary
To sum up, class attributes are shared among all instances of a certain class. Modify a class attribute and all instances will see the change.
Instance attributes are individual to a single instance. Modify an instance attribute and only this instance will have changed data
Puzzle Responses
Here are the responses to the above code puzzles.
## Puzzle 1: Eve(age 22) likes you! ## Puzzle 2: True ## Puzzle 3: False