IB Computer Science HL · Year 2 · Lesson 12

Class vs Instance State, Constructors & Objects

Distinguish shared class-level members from object-specific state, then construct Python classes and instantiate valid objects.

Paper 250 minutesB3.1.3 · B3.1.4
Targets

Shared state or object state?

2 / 10
  • distinguish static/class-level variables and methods from non-static/instance variables and methods, including their usage and scope;
  • decide when a value should be stored per object rather than shared by the class;
  • construct Python classes with constructors;
  • instantiate objects and trace how constructors initialize object state.
B3.1.3B3.1.4
Learn + retrieve

Rapid Recall Deck

3 / 10

Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.

B3.1.3 · Distinguish

Read class-level and instance-level members

4 / 10
class Student:
    school_name = "North Campus"

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

    def label(self):
        return self.name + " - Grade " + str(self.grade)

    @staticmethod
    def valid_grade(grade):
        return 6 <= grade <= 12

s1 = Student("Amina", 11)
s2 = Student("Leo", 12)

Classify each member

Class variable — shared at class level.
Instance variables — stored separately in each object.
Instance method — uses self.
Static method — no self; does not depend on one object’s state.
  • school_name: class variable.
  • name and grade: instance variables.
  • label(): instance method.
  • valid_grade(): static method.
Trace + reason

Scope determines where state belongs

5 / 10

Trace the state

class Badge:
    created = 0

    def __init__(self, owner):
        self.owner = owner
        Badge.created += 1

b1 = Badge("Maya")
b2 = Badge("Noah")

print(b1.owner)
print(b2.owner)
print(Badge.created)
Track each object separately.
Track the shared class variable once.

The output is Maya, Noah, then 2. Each object has its own owner, while Badge.created is shared and incremented by both constructor calls.

Decide where the data belongs

For a Course class, classify each item as class-level or instance-level: school name, course title, teacher name, number of courses created.

Likely class-level: a school name genuinely shared by every course; a count of course objects created. Instance-level: course title and teacher name because they can differ between course objects.

B3.1.4 · Construct

Define a class and instantiate objects

6 / 10
RequirementsA Device object must start with a serial number, model name and battery percentage. The battery percentage must be stored separately for every device.

Construct the class and two objects

Define class Device.
Use __init__ with three parameters.
Store all three as instance variables.
Create two different Device objects.
class Device:
    def __init__(self, serial, model, battery):
        self.serial = serial
        self.model = model
        self.battery = battery

d1 = Device("A104", "Tablet", 85)
d2 = Device("B205", "Laptop", 62)
Paper 2 reasoning

Constructor operations and design choices

7 / 10

What happens during instantiation?

For d1 = Device("A104", "Tablet", 85), list the important operations in order.

  1. A new Device object is created.
  2. The constructor __init__ runs with the supplied values.
  3. The object’s serial, model and battery attributes are initialized.
  4. The reference to the new object is assigned to d1.

Repair a bad design

A student stores battery = 100 as a class variable even though each device can have a different battery level. Explain the problem and correct the design.

Battery level is object-specific state, so it should be an instance variable such as self.battery initialized in the constructor. A class variable would incorrectly represent one shared battery value for all devices.

Exam lens

Teacher-created Paper 2 practice

8 / 10

Paper 2 mini-set

1. Distinguish a class variable from an instance variable.

2. A class Ticket must store a unique passenger name and price for each object. Construct the constructor and create one object.

3. Explain why passenger_name should be an instance variable rather than a class variable.

  1. Class variables are shared/class-level; instance variables belong to individual objects and can differ.
  2. class Ticket:
        def __init__(self, passenger_name, price):
            self.passenger_name = passenger_name
            self.price = price
    
    t1 = Ticket("Sofia", 240)
  3. Different tickets can belong to different passengers, so storing the name per object preserves independent state.
Exit check

Can you construct and classify members?

9 / 10

Exit construction

Add an instance method use_battery(amount) to Device that decreases self.battery by amount. Assume the input is valid.

def use_battery(self, amount):
    self.battery -= amount
Homework

Finish the learning cycle

10 / 10

IA — main task

Continue Criterion D implementation/testing. Capture one piece of evidence showing a completed feature or tested method.

Paper 2 — short practice

Without notes, reconstruct the Device class, create two objects and explain which variables are class-level versus instance-level.