Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookWhat Are Decorators?

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 1
some-alt