[Python-ideas] Syntax for defining parametric decorators

Nick Coghlan ncoghlan at gmail.com
Mon Jul 9 04:00:41 CEST 2012


On Mon, Jul 9, 2012 at 11:45 AM, Devin Jeanpierre
<jeanpierreda at gmail.com> wrote:
>> I'm having a lot of difficulty in thinking of how I would explain it to even
>> a moderately experienced user except by expanding it out to the explicit
>> nested function form. I'm not sure I can even explain it to myself.
>
> What's wrong with explaining it by expansion?
>
> Also, I doubt it's as hard as you claim. Something similar -- curried
> functions -- have been a staple of functional programming languages
> since before Python ever existed. This should also help explain it to
> lots of new users with wider backgrounds.

OK, I think it would be *much* better to approach the problem from that angle.

    def incremental(x)(f)(y):
        return x + f(y)

As equivalent to:

    # implied names are not legal identifiers - the compiler gets to do that
    # because of its privileged role in naming things
    def incremental(x):
        def <incremental:1>(f):
            def <incremental:2>(y):
                return x +f(y)
        return <incremental:1>

I'm still not convinced of the general applicability (since it breaks
down as soon as you want to decorate or otherwise pre- or post-process
any of the steps, thus forcing people to learn the full
"callable-returning-a-callable" idiom anyway), but it's probably worth
writing up as a PEP in the 3.4 timeframe.

Cheers,
Nick.

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



More information about the Python-ideas mailing list