[Python-ideas] Syntax for defining parametric decorators

Stephen J. Turnbull stephen at xemacs.org
Mon Jul 9 07:48:37 CEST 2012


Haoyi Li writes:

 > However, I think that in the general case, being able to define
 > curried functions in one line, a.l.a.
[...]
 > def f(a)(b)(c)
 > 
 > looks much more similar to how you would use it:
 > 
 > f(1)(2)(3)

I don't understand.  I wouldn't use it that way!  I would think if
that were common usage, and the curried form only occasional,

    def f(a,b,c):
        pass

    f(1,2,3)

would be more natural in Python (and use functools.partial for the
occasional currying).  Presumably if you were using this syntax, you'd
be doing something more like

    def f(a)(b)(c):
        pass

    def g(h, x):
        h(x)

    g(f(1)(2), 3)

which doesn't look very nice to me, nor do I find the def to be a
particularly intuitive way of reminding me that the proper usage is a
call of a function in curried form (specifically, I would not be
reminded of whether the expected curried form is a 1st-, 2nd-, or
3rd-order function).

I also suppose that you wouldn't be able to do

    def f(a)(b)(c):
        pass

    def g(x,y):
        pass

    h(f(1))
    h(g)

(ie, without LBYL or EAFP constructs in h, which would simply be
throwing the complexity into the caller's backyard).  Even though

    def bar(a)(b):
        pass

    def baz(x):
        pass

    quux(bar(1))
    quux(baz)

would work fine.

So to me, there may be something in this syntax, but my initial
impression is that it's trying to turn Python into something it's not.



More information about the Python-ideas mailing list