Python Interview Questions
Learn Python structures, decorators, memory management, and practical application logic.
How do Decorators work in Python?
A decorator is a function that takes another function and extends its behavior without modifying it explicitly. It allows you to wrap a function with additional logic, like checking if a user is logged in before letting them access a route.
Deep Dive
Because Python functions are first-class citizens (they can be passed as arguments), a decorator takes a function as an argument, defines a wrapper function inside it that executes the additional code (before or after the original function), and then returns the wrapper. We use the `@decorator_name` syntax above a function as syntactic sugar for `func = decorator_name(func)`.