Partially evaluated functions

Rainer Deyke root at rainerdeyke.com
Wed Jun 20 08:42:28 EDT 2001


"Nick Perkins" <nperkins7 at home.com> wrote in message
news:pDWX6.293707$eK2.59723440 at news4.rdc1.on.home.com...
>
> "Rainer Deyke" <root at rainerdeyke.com> wrote:
> > ...
> > class curry:
> >   def __init__(*args, **kwargs):
> >     self = args[0]
> >     self.function = args[1]
> >     self.args = args[2:]
> >     self.kwargs = kwargs
> >   def __call__(*args, **kwargs):
> >     kwargs.update(self.kwargs)
> >     return self.function(self.args + args, kwargs)
> > ...
>
> This is pretty close to the cookbook version:
>
> http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52549
> class curry:
>     def __init__(self, fun, *args, **kwargs):
>         self.fun = fun
>         self.pending = args[:]
>         self.kwargs = kwargs.copy()
>
>     def __call__(self, *args, **kwargs):
>         if kwargs and self.kwargs:
>             kw = self.kwargs.copy()
>             kw.update(kwargs)
>         else:
>             kw = kwargs or self.kwargs
>
>         return self.fun(*(self.pending + args), **kw)

My version is superior in that it doesn't use named arguments.  Consider:

def f(self):
  print self

curry(self = 5) # This will fail with the cookbook version.

> I notice that that the cookbook version makes a copy of the kwargs
> dictionary.
> I suppose this prevents kwargs from being modified after being supplied to
> curry.

Tests show that 'kwargs' is always a new object in the current
implementation, but I suppose that could change in the future.

> Also the actual call to the function uses the * and ** operators to
'expand'
> the arguments.

My version would have those too if I had been paying attention while I was
writing it.



--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list