← Python

OOP – Class Variables vs Instance Variables

A class variable is shared across all instances; an instance variable is unique to each object

What it does: Class variables are defined directly in the class body, outside any method. self.name stores data that belongs to one specific object. Instance variables use self — they're created in __init__ and belong to each object. count is defined on the class itself, so all Dog instances share the same count. self.name is unique to each dog; Dog.species is one value shared by all dogs. Modifying Dog.count changes it for every instance — it's the class-level value. Class variables suit things like instance counters or constants shared by all objects. Assigning self.species = 'Feline' creates an instance variable hiding the class one for that dog only.

Common mistakes: count should be a class variable, not instance. Define at class level. Redundant species is already a class variable. No need to set in __init__.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Where is a class variable defined?
Inside the class but outside methodsInside __init__
Matches
❌ species = 'Canine' # in __init__
Error: Class variables belong outside __init__
Applying
Meanings
Term
Class variable
Meaning
Variable defined in the class body and shared by all instances.
Practice
class Dog:
Testing
Gaps
= 'Canine'
Check Answer
Spots
3 self.name = name
4 self.count += 1 # track total dogs

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Lion's Mane Jellyfish' badge — Set Operator, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Where is a class variable defined?
A. Inside the class but outside methods ✓
B. Inside __init__
Class variables sit directly under the class line, not inside any method
2. Where is an instance variable defined?
A. Outside the class
B. Inside __init__ using self.var ✓
Instance variables use self — they're created in __init__ and belong to each object
3. class Dog:\n count = 0\nIs count shared between objects?
A. Yes it's a class variable ✓
B. No each object has its own
count is defined on the class itself, so all Dog instances share the same count
4. self.name vs Dog.species what's the difference?
A. They are the same
B. self.name is per-object, Dog.species is shared ✓
self.name is unique to each dog; Dog.species is one value shared by all dogs
5. Dog.count += 1 what happens?
A. Increments the shared counter ✓
B. Creates a new object
Modifying Dog.count changes it for every instance — it's the class-level value

Match the Pairs

species = 'Canine' # in __init__ Error: Class variables belong outside __init__
species = Canine Error: Missing quotes around string value
species = 'Canine' # in class body Valid: Class variable shared by all instances
self.name = name Ok: Instance variable unique to each object
bool(0) Output: False
bool([]) Output: False

Key Term

Class variable
Variable defined in the class body and shared by all instances.~~Instance variable

Fill in the Blanks

1. class Dog: species = 'Canine'
2. class Dog: count = 0 def __init__(self, name): Dog.count += 1
3. class Dog: count = 0 def __init__(self, name): self.count += 1
4. d = Dog('Rex') print(d.name)
5. print(Dog.species)

Spot the Error

Question 1
A class Dog:
B def __init__(self, name):
C self.name = name
D self.count += 1 # track total dogs
Line D — count should be a class variable, not instance. Define at class level.
Question 2
A class Dog:
B species = 'Canine'
C def __init__(self, name):
D species = 'Canine'
E self.name = name
Line D — Redundant species is already a class variable. No need to set in __init__.
Question 3
A class Dog:
B count = 0
C def __init__(self, name):
D self.name = name
E count += 1
Line E — Use Dog.count to modify the class variable count alone is undefined
Question 4
A class Dog:
B count = 0
C def __init__(self, name):
D self.name = name
E self.count += 1
Line E — self.count creates an instance variable that shadows the class variable
Question 5
A class Dog:
B species = 'Canine'
C
D d = Dog()
E d.species = 'Feline'
F print(Dog.species) # expects Feline
Line E — d.species = 'Feline' only changes that object. Use Dog.species to change for all.
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?