Decorator Syntax and Application
1234567891011def uppercase_decorator(func): def wrapper(): result = func() return result.upper() return wrapper @uppercase_decorator def greet(): return "hello, world" print(greet())
The @ symbol in Python is used to apply a decorator to a function in a clear and concise way. When you place @decorator_name directly above a function definition, Python automatically passes the function below to the decorator as an argument. This is exactly the same as writing function = decorator_name(function), but the @ syntax is more readable and is the standard way to apply decorators.
Decorators are applied at the time the function is defined, not when it is called. This means that as soon as Python encounters the decorated function in your code, it wraps the original function with the decorator, replacing the original function with the result returned by the decorator. As a result, the name of the function actually refers to the decorated version, not the original.
One important detail is that unless you take special steps (which you will learn later), applying a decorator can change the function's name and metadata. After decoration, the function’s __name__ will usually be the name of the wrapper function inside the decorator, not the original function name.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Can you explain how decorators work with functions that take arguments?
What happens if I apply multiple decorators to the same function?
How can I preserve the original function's metadata when using decorators?
Awesome!
Completion rate improved to 5.88
Decorator Syntax and Application
Pyyhkäise näyttääksesi valikon
1234567891011def uppercase_decorator(func): def wrapper(): result = func() return result.upper() return wrapper @uppercase_decorator def greet(): return "hello, world" print(greet())
The @ symbol in Python is used to apply a decorator to a function in a clear and concise way. When you place @decorator_name directly above a function definition, Python automatically passes the function below to the decorator as an argument. This is exactly the same as writing function = decorator_name(function), but the @ syntax is more readable and is the standard way to apply decorators.
Decorators are applied at the time the function is defined, not when it is called. This means that as soon as Python encounters the decorated function in your code, it wraps the original function with the decorator, replacing the original function with the result returned by the decorator. As a result, the name of the function actually refers to the decorated version, not the original.
One important detail is that unless you take special steps (which you will learn later), applying a decorator can change the function's name and metadata. After decoration, the function’s __name__ will usually be the name of the wrapper function inside the decorator, not the original function name.
Kiitos palautteestasi!