Class Decorator

say it: “klas DEK-uh-ray-ter” /klæs ˈdɛkəˌreɪtər/

A decorator placed on a class to change or register the whole class.

What is a class decorator?

A class decorator is a decorator placed on a class instead of a function, so it can change or register the whole class. @dataclass is one.

handlers = {}

def handles(event):
    def register(cls):
        handlers[event] = cls
        return cls
    return register

@handles("order_placed")
class SendReceipt:
    pass

print(handlers)  # {'order_placed': <class 'SendReceipt'>}

Level: Master · More Python terms at this level

Updated