OOP – Class Variables vs Instance Variables

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

OOP – Class Variables vs Instance Variables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where is a class variable defined?
A Inside the class but outside methods
B Inside __init__
2. Where is an instance variable defined?
A Outside the class
B Inside __init__ using self.var
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
4. self.name vs Dog.species what's the difference?
A self.name is per-object, Dog.species is shared
B They are the same
5. Dog.count += 1 what happens?
A Creates a new object
B Increments the shared counter

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1species = 'Canine' # in __init__ AValid: Class variable shared by all instances
2species = Canine BError: Class variables belong outside __init__
3species = 'Canine' # in class body COutput: False
4self.name = name DOutput: False
5bool(0) EOk: Instance variable unique to each object
6bool([]) FError: Missing quotes around string value

Definitions

Class variable

Fill in the Blanks

Complete each line of code by filling in the missing parts.

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

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Dog:
B def __init__(self, name):
C self.name = name
D self.count += 1 # track total dogs
Question 2
A class Dog:
B species = 'Canine'
C def __init__(self, name):
D species = 'Canine'
E self.name = name
Question 3
A class Dog:
B count = 0
C def __init__(self, name):
D self.name = name
E count += 1
Question 4
A class Dog:
B count = 0
C def __init__(self, name):
D self.name = name
E self.count += 1
Question 5
A class Dog:
B species = 'Canine'
C
D d = Dog()
E d.species = 'Feline'
F print(Dog.species) # expects Feline

Answer Key

OOP – Class Variables vs Instance Variables
Section A: Quick Questions
Q1
A — Inside the class but outside methods
Q2
B — Inside __init__ using self.var
Q3
A — Yes it's a class variable
Q4
A — self.name is per-object, Dog.species is shared
Q5
B — Increments the shared counter
Section B: Match the Pairs
Q1
species = 'Canine' # in __init__ → Error: Class variables belong outside __init__
Q2
species = Canine → Error: Missing quotes around string value
Q3
species = 'Canine' # in class body → Valid: Class variable shared by all instances
Q4
self.name = name → Ok: Instance variable unique to each object
Q5
bool(0) → Output: False
Q6
bool([]) → Output: False
Section C: Definitions
Q1
Class variable — Variable defined in the class body and shared by all instances.~~Instance variable
Section D: Fill in the Blanks
Q1
class Dog: species = 'Canine'
Q2
class Dog: count = 0 def __init__(self, name): Dog.count += 1
Q3
class Dog: count = 0 def __init__(self, name): self.count += 1
Q4
d = Dog('Rex') print(d.name)
Q5
print(Dog.species)
Section E: Spot the Error
Q1
D — count should be a class variable, not instance. Define at class level.
Q2
D — Redundant species is already a class variable. No need to set in __init__.
Q3
E — Use Dog.count to modify the class variable count alone is undefined
Q4
E — self.count creates an instance variable that shadows the class variable
Q5
E — d.species = 'Feline' only changes that object. Use Dog.species to change for all.