What are Python decorators?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Python topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
A decorator is a function that takes another function as an argument, adds some behaviour, and returns the modified function. The @decorator syntax is syntactic sugar for func = decorator(func). A decorator using functools.wraps: from functools import wraps; def timer(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time(); result = func(*args, **kwargs); print(time.time() - start); return result; return wrapper. Apply: @timer def my_function(): .... Built-in decorators: @staticmethod, @classmethod, @property. Decorators are widely used for logging, timing, authentication, caching (@functools.lru_cache), and input validation. Multiple decorators stack from bottom to top.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last Python project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What are Python list comprehensions?
Next
What is the difference between is and == in Python?