October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

What Python’s @ Syntax Changes When a Definition Runs

Python decorators transform functions or classes when their definitions execute. Here’s how rebinding, stacked decorators, argument-taking factories, and class decorators work.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

@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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why 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.

  • 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.