scoping weirdness

Tom Good Tom_Good1 at excite.com
Thu Aug 30 19:02:10 CEST 2001


"Alex Martelli" <aleax at aleax.it> wrote in message news:<9mkt0t01cno at enews1.newsguy.com>...
> "Tom Good" <Tom_Good1 at excite.com> wrote in message
> news:ac677656.0108291503.4d211cd6 at posting.google.com...
> > "Alex Martelli" <aleax at aleax.it> wrote in message
>  news:<9md62v01ne3 at enews1.newsguy.com>...
> > > Currying-to-argumentlessness is a frequent enough need
> > > (since Tk &c all want lots of argumentless callables)
> > > that one may want to rename 'curry_to_argumentlessness'
> > > to something more concise and put it in sitecustomize
> > > or thereabouts.
> >
> > How about 'curry_all'?
> >
> > . . . 'curry_extra_spicy' comes to mind, too, but it is too many
> characters :-)
> 
> Another frequent need, almost as frequent as currying all
> arguments, is currying all but one, to get a one-argument
> callable suitable for filter (or map -- it seems to me that
> most of my map calls are with 1-arg callables).  Problem
> with this is that there's no obvious syntax to specify
> which one argument you want left "free" (uncurried) -- I
> think that hardwiring it to the first one would cover a
> majority of cases, but far from all.   Hmmmm....
> 
> 
> Alex

Currying all but the first one probably would work most of the time. 
For a little more flexibility, maybe specify by index which argument
to leave free:


# ---- begin code ----

from __future__ import nested_scopes

def curry_all_but(index, *args, **kwds):
    def curried_func(single_arg):
        return args[0](*(args[1:index] + (single_arg,) + 
                         args[index:]), **kwds)
    return curried_func

def test(a, b):
    print "you chose %s, %s" % (a,b)

if __name__ == "__main__":
    f1 = curry_all_but(1, test, 'B')
    f1('A')

    f2 = curry_all_but(2, test, 'B')
    f2('A')

# ---- end code ----

Output:

you chose A, B
you chose B, A



More information about the Python-list mailing list