Interview Question

What are decorators?

Decorators replace a defined function or class through a callable transformation.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

A decorator transforms a function or class definition using callable syntax. • `@decorator` applies the decorator to the object created by the following definition. • A function decorator receives the function and returns a replacement object. • functools.wraps helps preserve wrapped function metadata.

💡 Simple Example

def uppercase(function): def wrapper(): return function().upper() return wrapper @uppercase def greet(): return 'hello' print(greet())

Output

HELLO

⚡ Quick Revision

Decorators replace a defined function or class through a callable transformation.