How decorators compose
When stacking decorators, the bottom one wraps first:
@a
@b
@c
def f(): ...
# equals a(b(c(f)))
Each layer adds behaviour. Order matters: an outer decorator sees the already-wrapped inner function.
Metaclasses overview
A class is an instance of its metaclass; the default is type. Rarely needed, but metaclasses control class creation globally:
class Meta(type):
def __new__(cls, name, bases, ns):
ns["tag"] = True
return super().__new__(cls, name, bases, ns)
class Foo(metaclass=Meta):
pass
print(Foo.tag) # True
Prefer classmethods or simple decorators over metaclasses unless you must change class creation itself.