01 / 21

Class vs. Instance
Attributes

Understand how Python objects share data through class attributes and hold unique data through instance attributes: the foundation of object-oriented design.

Python

What Are Attributes?

In Python, an attribute is any variable attached to an object or class. Think of attributes as named slots that store data. Every time you write dog.name or Dog.species, you are accessing an attribute.

Class Attribute
Defined directly inside the class body. Shared by all instances.
Instance Attribute
Defined inside __init__ using self. Unique to each object.

The Blueprint vs. the House

class Dog species = "Canis" Shared by all dogs CLASS ATTRIBUTE dog_1 name = "Rex" age = 3 dog_2 name = "Luna" age = 5 dog_3 name = "Buddy" age = 1 INSTANCE ATTRIBUTES

Why Does This Matter?

Choosing the right type of attribute affects memory usage, data consistency, and bug frequency. A single misplaced attribute can cause every object in your program to share data you intended to be private.

Memory Efficiency
Class attributes are stored once, not duplicated across instances.
Shared Configuration
Constants, default settings, and counters belong at the class level.
Avoiding Bugs
Mutable class attributes (like lists) can cause unintended sharing.

The Core Rule

class Dog: # Class attribute: shared by ALL instances species = "Canis familiaris" def __init__(self, name, age): # Instance attributes: unique to EACH object self.name = name self.age = age

Python's Lookup Chain

1. Instance __dict__ Check the object first not found? 2. Class __dict__ Check the class next not found? 3. AttributeError Example dog_1.name → found in instance ✓ dog_1.species → not on instance, found on class ✓ dog_1.color → AttributeError

Class vs. Instance: Side by Side

Class Attribute Instance Attribute
Where defined In the class body In __init__ via self
Ownership Belongs to the class Belongs to each object
Shared? Yes: all instances No: unique per object
Access via ClassName.attr or obj.attr obj.attr only
Use case Constants, counters, defaults Object-specific data

How to Choose the Right Type

1
Ask: "Is this the same for every instance?" If yes, use a class attribute (e.g., a species name or a configuration flag).
2
Ask: "Does each object need its own copy?" If yes, use an instance attribute via self in __init__.
3
Ask: "Is the value mutable (list, dict, set)?" If yes, almost always make it an instance attribute. Mutable class attributes are shared references and cause bugs.
4
Ask: "Do I need to count all instances?" A counter incremented in __init__ should be a class attribute, because it tracks the class, not one object.

Example 1: Instance Counter

class Student: count = 0 # class attribute def __init__(self, name): self.name = name # instance attribute Student.count += 1 # update class attribute a = Student("Alice") b = Student("Bob") print(Student.count) # 2

Example 2: Shadowing a Class Attribute

class Config: theme = "dark" # class attribute c1 = Config() c2 = Config() c1.theme = "light" # creates INSTANCE attribute on c1 print(c1.theme) # "light" (instance) print(c2.theme) # "dark" (class) print(Config.theme) # "dark" (class)

Example 3: The Mutable Trap

class Team: members = [] # DANGER: mutable class attribute def add(self, name): self.members.append(name) t1 = Team() t2 = Team() t1.add("Alice") print(t2.members) # ["Alice"] — t2 sees t1's data!

The Correct Pattern

class Team: max_size = 5 # immutable: safe as class attr def __init__(self, name): self.name = name self.members = [] # mutable: instance attr ✓ def add(self, person): if len(self.members) < self.max_size: self.members.append(person)

Memory Model

Team (class object) max_size = 5 stored once in memory t1 (instance) name = "Alpha" members = ["Alice"] own list object: 0x1A t2 (instance) name = "Bravo" members = ["Bob"] own list object: 0x2B Each instance has its own members list max_size is read from the class via lookup chain

When to Use Class vs. Instance

Class Attributes
Constants: \( \pi = 3.14159 \)
Counters: count = 0
Default configs: debug = False
Class-level metadata
Instance Attributes
Identity: self.name
State: self.score
Mutable collections: self.items = []
Per-object configuration

Common Mistakes

✕ Mutable Class Attribute
Using a list or dict as a class attribute. All instances share the same mutable object, leading to data leaking between objects.
✕ Using self to Increment
Writing self.count += 1 instead of ClassName.count += 1. This shadows the class attribute with a new instance attribute.
✕ Forgetting self
Defining a variable in __init__ without self. makes it a local variable that disappears after __init__ returns.

Quick Reference Summary

Question Answer
Where are class attrs stored? In the class's __dict__
Where are instance attrs stored? In the object's __dict__
What does assignment on an instance do? Creates/updates an instance attribute (shadows class attr)
Are mutable class attrs safe? No: shared reference causes bugs
How to modify a class attr properly? Use ClassName.attr = value

Question 1 of 5

Which of the following is a class attribute?

Question 2 of 5

Given the code below, what does print(b.x) output?

class Foo: x = 10 a = Foo() b = Foo() a.x = 99 print(b.x)

Question 3 of 5

Why is defining members = [] as a class attribute dangerous?

Question 4 of 5

To properly increment a class-level counter inside __init__, you should write:

Question 5 of 5

What happens when Python looks up obj.x and x is not in the instance's __dict__?