class is a call¶
Level: 301 · for Python programmers, especially ones coming from ABAP, Rust or C
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
One line: A class statement runs its body top to bottom like a function, collects the names it bound into a dict, and calls type(name, bases, dict) — so a class is an object made at run time, a class decorator or a metaclass is only a different callee, and __init_subclass__ gets you most of what a metaclass does without writing one.
What the finished page has to answer¶
type(C) is type, andtype('C', (Base,), {'x': 1})making the same class the statement would, measured side by side.- The body is code: a
print, a loop or anifin a class body runs when the class is made, once, at import. - The hooks in the order they fire:
__prepare__, the body,__set_name__on each descriptor,__init_subclass__on the parent, then the metaclass's__new__and__init__, and later__call__on every instantiation, which is where a singleton or an instance cache lives. __init_subclass__(PEP 487) as the ordinary tool for registries and validation, and the short list of things that still need a metaclass.- A class is a value: kept in a dict, returned from a function, made in a loop. A decorator is a call applies to
@dataclassand to every other class decorator. - The contrast that makes the page: in ABAP a class is a repository object activated before anything runs, in Rust and C a type does not exist at run time at all.
See also¶
- A decorator is a call, made when
defruns: the function-side version of a callee you did not see - Customizing class creation ↗ in the language reference,
type↗, and PEP 487 ↗ - A type is not a constructor ↗ in the Rust library: the other end of the same axis