DEV Community

Cover image for Python by Structure: Decorators and the "Logic Envelope"
Aaron Rose
Aaron Rose

Posted on

Python by Structure: Decorators and the "Logic Envelope"

"I feel like a broken record, Margaret," Timothy said, rubbing his eyes. "I have ten different functions, and I’ve had to copy-paste the same logging lines into every single one of them. If the boss wants the log prefix changed from 'LOG:' to 'DEBUG:', I’m going to be here all night."

Margaret smiled, pulling up the Python Structure Viewer. "You’re treating your cross-cutting concerns—like logging—as if they are part of the function's core job. They aren't. They are the 'envelope' the function travels in."

The Problem: Intertwined Logic

"Let's look at your 'Manual' way," Margaret said, pasting Timothy's repetitive code into the Viewer.

Timothy’s "Manual" Code:

def calculate_total(price, tax):
    print("LOG: Calling calculate_total")
    result = price + (price * tax)
    print("LOG: Finished calculate_total")
    return result

Enter fullscreen mode Exit fullscreen mode

Python Structure Viewer output:

=== TREE VIEW ===

calculate_total(price, tax)
    print('LOG: Calling calculate_total')
    result = price + price * tax
    print('LOG: Finished calculate_total')
    Return result


=== ENGLISH VIEW ===

Function calculate_total(price, tax):
  Evaluate print('LOG: Calling calculate_total').
  Set result to price + price * tax.
  Evaluate print('LOG: Finished calculate_total').
  Return result.

Enter fullscreen mode Exit fullscreen mode

"See how the print statements are inside the function body?" Margaret pointed at the screen. "In the Tree View, they are at the same level as your math. You’ve mixed the 'What' (calculating tax) with the 'How' (logging the call). They are structurally inseparable."

The Solution: The Structural Envelope

"Now," Margaret said, "watch what happens when we use a Decorator. We extract that logging logic into its own wrapper function and simply apply it with the @ symbol."

The Structured Python Snippet:

@logger
def calculate_total(price, tax):
    return price + (price * tax)

Enter fullscreen mode Exit fullscreen mode

Python Structure Viewer output:

=== TREE VIEW ===

@logger
calculate_total(price, tax)
    Return price + price * tax


=== ENGLISH VIEW ===

Decorator @logger
Function calculate_total(price, tax):
  Return price + price * tax.

Enter fullscreen mode Exit fullscreen mode

Timothy blinked. "The Viewer literally moved it outside. It says 'Decorator @logger' and then starts the function description below it."

"Exactly!" Margaret said. "The @logger isn't part of the function’s internal logic anymore. It’s a structural instruction that says: 'Before you run this function, run the logger logic.' The function itself stays pure—it only contains the math."

The "Envelope" Metaphor

"It's like a letter and an envelope," Timothy realized. "The letter is the function—the actual message. The envelope is the decorator. It handles the 'addressing' and 'postage' (the logging), but it doesn't change the words inside the letter."

"Perfect," Margaret nodded. "By looking at the structure, you can see that the decorator is a separate layer. You can swap envelopes or change the return address without ever touching the letter itself."

Timothy looked back at his ten functions. "So if I want to change the prefix, I only have to change it once—inside the @logger definition—and every function wearing that 'envelope' is updated instantly."

"That," Margaret said, "is the power of structural separation."


Analyze Python structure yourself

Download the Python Structure Viewer — a free tool that shows code structure in tree and plain English views. Works offline, no installation required.


Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.

Top comments (0)