Metaclasses — usually not

A class is an object whose type is type. A metaclass customises class creation. Internship code almost never needs one; a decorator or a factory is the first tool.

Library desk

A fee function you can test.

You will be able to

  • Know a class is an object whose type is type
  • Treat a metaclass as custom class creation — almost never needed
  • Reach for a factory or a decorator first

Internship code almost never needs a metaclass

On a laptop, class Room: makes an object whose type is type. A metaclass customises that birth: inject a campus stamp, register plugins, forbid a missing method. Frameworks sometimes do this. Your attendance CSV does not.

If you wanted every sensor class to carry campus = "RSIL Demo", a class attribute, a decorator, or a factory will do. A metaclass is how you get a 2 a.m. stack trace nobody on the floor can read.

Look, then put it back

The sample below is so you can recognise metaclass= in a library, not so you start the next intern project with one. Copy it onto a laptop if you must. This Run box has no type() gymnastics — only a registry dict, which is the factory you actually wanted.

Laptop only — a tiny metaclass you should usually not write

class Kind(type):
    def __new__(cls, name, bases, body):
        body.setdefault("campus", "RSIL Demo")
        return super().__new__(cls, name, bases, body)

class Sensor(metaclass=Kind):
    pass

print(Sensor.campus)

# Prefer this instead:
def make_sensor(sid):
    return {"id": sid, "campus": "RSIL Demo"}

Words that matter

type
The usual type of classes — classes are objects too.
Metaclass
A class of a class; customises class creation.
Registry / factory
The first tool when you wanted ‘magic at birth’.

Common mistakes

Avoid: Starting a campus tool with a metaclass because a blog used one.

Do this: A factory or a decorator; metaclasses when a framework forces you.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — a registry is enough

Kinds of sensor as data. No class creation hook.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — stamp campus on the record

What the metaclass would have injected — as a function.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Example program — Register, then build

Plugin energy without type().

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Campus stamp

Print a line that includes both RSIL Demo and factory.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. A metaclass is usually…
2. If every record needs campus = RSIL Demo…

Progress is stored in a browser cookie on this device.