Classes and objects
A class is a recipe; each instance gets its own attributes while methods live once on the class.
A list carries both data and operations. So does a file. A class lets you build your own type with that same property: the data each object holds, and the methods that know how to operate on it, bundled under one name. The first object-oriented program is usually just a small record with a couple of behaviours.
Recipe versus cake
The class is the recipe: written once, shared by every object of that type.
Each instance is a cake — its own ingredients, its own state. Methods are
steps in the recipe that receive the particular instance they are called on,
which is what the first parameter self is for.
Create a few counters below, then change one and notice that the others and the shared class-level total behave differently.
Create instances, then change one and watch the others stay put
class attribute
Counter.created = 0
instances
0 objects
No instances yet — press Counter().
statement log
—
value is stored per instance; created lives on the class object and is shared. A method receives its own instance as self, which is the mechanism that lets .increment() change one counter without touching the others.
Instances hold state; the class holds behaviour
A minimal class defines what to do when an object is created:
class Counter:
created = 0 # class attribute, shared
def __init__(self, start=0):
self.value = start # instance attribute, per object
Counter.created += 1
def increment(self, by=1):
self.value += by
return self.valueCounter() calls __init__ automatically. self is bound to the new
instance, so self.value is that object's own attribute. created is looked up
on the class and is the same for everyone.
Class attributes versus instance attributes
When Python reads obj.attr it looks first in the instance's __dict__, then
on the class. When you assign obj.attr = ..., you almost always create or
update an instance attribute, even if a class attribute of the same name
exists. To change the shared one you must assign through the class:
Counter.created += 1.
Mutable class attributes are shared
A class attribute that is a list, dict or other mutable object is shared by
every instance. class Team: members = [] means all teams append to the same
list. Use an instance attribute set in __init__ when each object needs its
own copy.
Methods are functions with a receiver
A method is a plain function whose first parameter is the instance. c.increment(2)
is exactly Counter.increment(c, 2). This is why forgetting self in the
definition produces a TypeError about arguments at call time — the receiver is
passed positionally whether you asked for it or not.
Attributes become available as soon as __init__ assigns them, which is why
__init__ is where invariants belong: validate inputs there, and every later
method can assume them.
Illustrative vs real
The explorer shows a single integer attribute so the state fits a card. Real
classes hold many attributes, compose other objects, and define dunder methods
such as __repr__ and __eq__ so they print and compare sensibly. The
division of labour — class for behaviour, instance for state — is unchanged.
Check yourself
Eduspheria wiki · Programming & Data Structures, Objects and I/O
0 / 5 answered
From the exam paper
Modeled on NITJ AI-503, End-Sem December 2024
0 / 4 answered
Where next: inheritance — building new classes on top of existing ones and letting one call site work with many types.