[Python-ideas] Syntax for defining parametric decorators

Nick Coghlan ncoghlan at gmail.com
Tue Jul 10 08:32:24 CEST 2012


On Mon, Jul 9, 2012 at 7:47 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> >From an innocent look, I have no idea what the syntax is supposed to mean.
> Clearly doesn't hint at a factory for me.

I should also mention that I have a different proposal that affects
the way one would write functions-that-returns-functions. I've been
messing around with the idea of statement local namespaces for years
(see PEP 3150) trying to find something that I consider better than
the status quo, and PEP 403's statement local function and class
definitions (http://www.python.org/dev/peps/pep-0403/) are the current
incarnation.

With those, the number of statements in a simple wrapping decorator
factory doesn't change, but the return statements can be moved above
their respective function definitions:

    def notify_on_call(callback, *cb_args, **cb_kwds):
        in return decorator
        def decorator(f):
            in return wrapped
            @functools.wraps(f)
            def wrapped(*args, **kwds):
                callback(*cb_args, cb_kwds)
                return f(*args, **kwds)

Rather than the current out-of-order:

    def notify_on_call(callback, *cb_args, **cb_kwds):
        def decorator(f):
            @functools.wraps(f)
            def wrapped(*args, **kwds):
                callback(*cb_args, cb_kwds)
                return f(*args, **kwds)
            return wrapped
        return decorator

(Note: I haven't updated the PEP in a while, so it currently still
disallows combining the in statement with decorators - I think that's
a mistake, and will be updating it some time post 3.3)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list