Partially evaluated functions

Alex Martelli aleaxit at yahoo.com
Wed Jun 20 02:14:36 EDT 2001


"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:csVX6.324433$oc7.30746857 at news2.rdc2.tx.home.com...
> "Venkatesh Prasad Ranganath" <rvprasad at cis.ksu.edu> wrote in message
> news:m3sngw12ce.fsf at boss.dreamsoft.com...
    ...
> > Is partially evaluated functions possible in python?  If there has been
a
> > discussion about it, where can I find the jist of the discussion?
> >
> > def conv(a, b, c):
> >     return (a + b) * c
> >
> > faren2cel = conv(b = -32, c = 5 / 9)
>
> class curry:
    ...
> faren2cel = curry(conv, b = -32.0, c = 5.0 / 9)

The class-based solution is most idiomatic in Python, but closures
are also possible -- in Python 2.2 (or 2.1 + 'from future import'):

def curry(func, *args, **kwds):
    def callit(*moreargs, **morekwds):
        morekwds.update(kwds)
        return func(*(moreargs+args), **morekwds)
    return callit

you may of course choose different strategies for combining the
originally supplied plain & named arguments and those given at
the point of call.  In Python 2.0 (or 2.1 without 'from future'), it
is not as easy when the function you want to call can have any
signature whatsoever, as the generic signatures make it hard to
inject names from curry down into callit's namespace explicitly,
as needed.  It's fine if don't need this total genericity, but if you
do and also want to run on older Python versions, I'd stick with
some class-based solution.  new.function can also be useful, if,
again, you don't need to support total genericity.


Alex






More information about the Python-list mailing list