Python Decorators

In python, every function is an object

def shout(text):
	return text.upper()

f = shout          # no parentheses: we're referencing the function, not calling it
print(f("hi"))     # HI

A decorator is a function that takes a function and returns a replacement for it. The @ syntax is just shorthand:

@my_decorator
def greet(): ...

# is exactly the same as:
def greet(): ...
greet = my_decorator(greet)

@dataclass is a class decorator. It reads the class’s type annotations and generates methods you’d otherwise write by hand:

@dataclass
class Task:
    name: str
    fn: Callable
    deps: list[str] = field(default_factory=list)

That’s roughly equivalent to writing:

class Task:
    def __init__(self, name, fn, deps=None):
        self.name = name
        self.fn = fn
        self.deps = deps if deps is not None else []
    def __repr__(self):
        return f"Task(name={self.name!r}, fn={self.fn!r}, deps={self.deps!r})"
    def __eq__(self, other):
        return (self.name, self.fn, self.deps) == (other.name, other.fn, other.deps)