[Python-ideas] Decorator syntax restriction

Carl Johnson cmjohnson.mailinglist at gmail.com
Sat Oct 10 14:25:15 CEST 2009

Stephen J. Turnbull:

> Does either of the suggestions for debug actually "work",
> stylistically?  Specifically, consider the preparatory code required.

I had some questions about that myself, but I don't think they're that
deep. One could easily write something like:

from debugger import debug_dec

debug_flag = True #Or False

@debug_dec if debug_flag else lambda f: f
def my_func(arg1): …

I was only using "debug" as both a flag and a function because it was
used that way in the previously given example. I see a certain
elegance in using "debug" as both a flag and a function, but there's
also something a little unsettling about it that others might not

Now, a legitimate follow up question might be, why not do this, which
is legal today:

from debugger import debug_dec

debug_flag = True #Or False

if not debug_flag: debug_dec = lambda f: f

def my_func(arg1): …

I think the counter-objection here will be, "Why should I have
@debug_dec (or in Stephen's example @instrument) in front of my
function if it's only sometimes a debug decorator and other times an
identity function?" And I think that's a good counter-objection. The
reason that we have decorators at the top of functions instead of the
bottom is to improve readability. But when @debug_dec only means
"sometimes this will apply a debug decorator but not necessarily" that
seems very misleading to me. Why not keep the "if" at the top so it
doesn't get passed over because the next programmer only read the
function declaration but not the actual definition of

At this point though, instead of debating subjective stylistic
questions I really do think that something like the consenting adults
clause should come into play. Why enforce debatable style decisions
through arbitrary grammar restrictions?

— Carl

More information about the Python-ideas mailing list