Class vs Instance State, Constructors & Objects
Distinguish shared class-level members from object-specific state, then construct Python classes and instantiate valid objects.
Shared state or object state?
- 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.
Rapid Recall Deck
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”.
Read class-level and instance-level members
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
self.self; does not depend on one object’s state.school_name: class variable.nameandgrade: instance variables.label(): instance method.valid_grade(): static method.
Scope determines where state belongs
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)
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.
Define a class and instantiate objects
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
class Device.__init__ with three parameters.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)Constructor operations and design choices
What happens during instantiation?
For d1 = Device("A104", "Tablet", 85), list the important operations in order.
- A new
Deviceobject is created. - The constructor
__init__runs with the supplied values. - The object’s
serial,modelandbatteryattributes are initialized. - 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.
Teacher-created Paper 2 practice
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.
- Class variables are shared/class-level; instance variables belong to individual objects and can differ.
class Ticket: def __init__(self, passenger_name, price): self.passenger_name = passenger_name self.price = price t1 = Ticket("Sofia", 240)- Different tickets can belong to different passengers, so storing the name per object preserves independent state.
Can you construct and classify members?
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 -= amountFinish the learning cycle
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.