Python’s @decorator syntax transforms a function or class when its definition executes, then binds the defined name to the result. It is not inherently suspicious: it puts a transformation beside the declaration, though stacked or opaque decorators can make behavior harder to follow.
What does @decorator do?
For a function definition, Python creates the function and applies the decorator to it. The name is then bound to whatever object the decorator returns. In the simple case, this is equivalent to defining the function and then assigning the result of a call back to its name:
As an Amazon Associate I earn from qualifying purchases.
@decorator
def func():
...
def func():
...
func = decorator(func)
This is the language’s definition-time transformation and rebinding mechanism. The decorated result—not necessarily the original function—is what func refers to afterward. The Python language reference describes decorator evaluation as part of executing a function definition: Compound statements — Python 3.14.8 documentation.
Why put the transformation above the definition?
Before decorator syntax, a transformation such as making a method a class method could be written as a reassignment after the function body. That works, but it separates the transformation from the declaration it changes. The @ form makes that relationship visible at the point where a reader encounters the definition.
#1 Best Overall
| Form | Where the transformation appears | What a reader still needs to discover |
|---|---|---|
@decorator above a definition |
Beside the function or class declaration | What the decorator does and what object it returns |
| Explicit reassignment after a definition | After the function or class body | Which earlier definition the reassignment changes and how it changes it |
PEP 318, the proposal for function and method decorators, says the earlier transformation style “is awkward and can lead to code that is difficult to understand.” Its authors’ design goal was to put the transformation near the declaration, not to guarantee that every decorator is self-explanatory. Readability depends on the decorator’s name, implementation, and placement. PEP 318.
How does the order of stacked decorators work?
When decorators are stacked, Python applies them from the one closest to def outward. In other words, the stack composes bottom to top:
Rank #2
@dec2
@dec1
def func():
...
This is equivalent to:
def func():
...
func = dec2(dec1(func))
So dec1 receives the original function first, and dec2 receives the result of dec1. The order can change behavior, so inspect the stack from the bottom upward when working out what a decorated name refers to. PEP 318.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11When does decoration happen?
Decoration happens when execution reaches the definition statement—not each time the resulting function is called. Afterward, the definition’s name refers to the decorated result. If the definition is inside a function or a conditional block, that definition executes when the surrounding flow reaches it; it is not a one-time transformation performed merely because Python parsed the file.
Can a decorator take arguments?
Yes. The expression after @ can call a factory that returns a decorator. Conceptually, @decomaker(arg) means that Python calls decomaker(arg) and applies the returned decorator to the newly defined function. The argument-taking expression supplies configuration to the decorator factory; it is not itself the function transformation. PEP 318 describes this form and the syntax’s constraints: PEP 318.
Do decorators work on classes too?
Yes. Python supports decorators on class definitions as well as function definitions. A class decorator receives the newly created class and the class name is bound to the decorator’s result, just as a function name is bound to the result of function decoration. Class decorators arrived in Python 3.0; the current language reference covers their use, and PEP 3129 records their introduction: Python language reference and PEP 3129 — Class Decorators.
Class decorators and metaclasses can both be involved in changing classes, but they are not interchangeable in every case. PEP 3129 notes that trying to obtain decorator-like functionality through metaclasses can be unpleasant and fragile; choose a mechanism based on the transformation needed rather than assuming one is a drop-in substitute for the other.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsWhy do decorators feel “sus” sometimes?
The syntax is explicit, but the transformation may not be. A short decorator name can conceal substantial behavior, and several stacked decorators require readers to track a chain of returned objects. That is a reason to inspect unfamiliar decorators, not evidence that @ itself is unsafe or deceptive.
Best Value
- Look up the decorator’s implementation or documentation when its effect is not clear from its name.
- For a stack, start with the decorator nearest the definition and follow the returned object outward.
- When defining a decorator, make its purpose and the behavior it changes apparent to people reading the declaration.
Where did Python’s decorator syntax come from?
PEP 318 proposed decorators for functions and methods, which were introduced in Python 2.4. PEP 3129 proposed class decorators, added in Python 3.0. These are historical milestones; they do not determine whether a particular decorator is a good fit for a modern codebase. PEP 318 and PEP 3129.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




