Concepts 🚀 Project 🧮 Labs
📚 Learn / Python / Classes & __init__

Classes & __init__

Blueprints for objects. __init__ runs when an instance is created; self is the instance.

Code example

class SessionState:
    def __init__(self, user_id: int):
        self.user_id = user_id
        self.namespace = {}
        self.cell_count = 0

    def touch(self):
        self.cell_count += 1

s = SessionState(user_id=1)
s.touch()

How it works

A class is a blueprint. `__init__` is the constructor — it runs every time you call `ClassName(...)`. `self` is the instance being constructed (like `this` in JS). Methods are just functions defined inside the class body; their first parameter is always the instance. The notebook's _SessionState class is a perfect example: each user gets one instance, and the per-user namespace dict lives on `self`.

🔍 Where this is used in OpenBenchML app/routes/notebook.py — _SessionState is the per-user kernel state.