Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen What Are Decorators? | Decorator Fundamentals
Quizzes & Challenges
Quizzes
Challenges
/
Python Decorators Explained

bookWhat Are Decorators?

In Python, functions are considered first-class objects. This means you can assign functions to variables, pass them as arguments to other functions, and return them from functions, just like any other object. This flexibility allows you to write higher-order functions—functions that take other functions as arguments or return them as results. Higher-order functions are a key feature that enables decorators in Python.

123456789101112
def simple_decorator(func): def wrapper(): print("Before the function runs.") func() print("After the function runs.") return wrapper @simple_decorator def greet(): print("Hello, world!") greet()
copy

In the code above, the simple_decorator function takes another function func as its argument. Inside simple_decorator, a new function called wrapper is defined. This wrapper function prints a message before and after calling the original function func. The simple_decorator then returns this wrapper function.

When you use the @simple_decorator syntax above the greet function, Python automatically passes greet to simple_decorator and replaces greet with the returned wrapper. As a result, calling greet() actually calls the wrapper, which adds the extra behavior before and after the original greet function runs. This is the essence of how decorators work in Python: they allow you to wrap additional functionality around existing functions in a clean and reusable way.

question mark

Which statement best describes the role of higher-order functions in enabling decorators in Python?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookWhat Are Decorators?

Swipe um das Menü anzuzeigen

In Python, functions are considered first-class objects. This means you can assign functions to variables, pass them as arguments to other functions, and return them from functions, just like any other object. This flexibility allows you to write higher-order functions—functions that take other functions as arguments or return them as results. Higher-order functions are a key feature that enables decorators in Python.

123456789101112
def simple_decorator(func): def wrapper(): print("Before the function runs.") func() print("After the function runs.") return wrapper @simple_decorator def greet(): print("Hello, world!") greet()
copy

In the code above, the simple_decorator function takes another function func as its argument. Inside simple_decorator, a new function called wrapper is defined. This wrapper function prints a message before and after calling the original function func. The simple_decorator then returns this wrapper function.

When you use the @simple_decorator syntax above the greet function, Python automatically passes greet to simple_decorator and replaces greet with the returned wrapper. As a result, calling greet() actually calls the wrapper, which adds the extra behavior before and after the original greet function runs. This is the essence of how decorators work in Python: they allow you to wrap additional functionality around existing functions in a clean and reusable way.

question mark

Which statement best describes the role of higher-order functions in enabling decorators in Python?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1
some-alt