What 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.
123456789101112def 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()
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.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 5.88
What 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.
123456789101112def 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()
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.
Danke für Ihr Feedback!