Partially evaluated functions
Rainer Deyke
root at rainerdeyke.com
Thu Jun 21 00:13:10 EDT 2001
"Nick Perkins" <nperkins7 at home.com> wrote in message
news:abeY6.301566$eK2.61312477 at news4.rdc1.on.home.com...
>
> Rainer Deyke:
> > > >>> def f(self, **kwargs):
> > > ... print self, kwargs
> > > ...
> > > >>> f(f, self=5)
> > > Traceback (most recent call last):
> > > File "<stdin>", line 1, in ?
> > > TypeError: keyword parameter redefined: self
>
>
> Carsten Geckeler:
>
> > Probably you mean
> > curry(f, self=5)
> > and not
> > f(f, self=5)
> >
> > But you are right that you get a TypeError due to the __init__ function
in
> > the class.
>
>
> Of course you get an error when you try to pass a keyword argument to a
> function that does not take any keyword arguments.
Read the example again. I am trying to get 'self : 5' into 'kwargs'.
> ( and why are you trying to pass 'self'?)
The name is irrelevant.
> What does work, for the cookbook version, and Alex's functional version,
is
> any of the following:
>
> def print_thing( thing='boat' )
> print thing
> print_plane = curry( print_thing, thing='plane' )
> print_plane()
>
> ..now calling print_plane() will simply print 'plane'
>
>
> or, the same thing without the keywords:
>
> def print_thing( thing ):
> print thing
> print_plane = curry( print_thing, 'plane' )
> print_plane()
>
>
> or.. you can even do this:
>
> def print_thing( thing='boat' ):
> print thing
> print_plane = curry( print_thing, 'plane' )
> print_plane()
>
> ..with a positional arg being 'promoted' to a keyword arg.
This also works:
def print_thing(thing):
print thing
print_plane = curry(print_thing, thing = 'plane')
print_plane()
This doesn't work:
def print_fun(fun = 'boat'):
print 'A %s is fun.' % fun
print_plane = curry(print_fun, fun = 'plane')
print_plane()
Neither does the obvious variation:
def print_fun(fun):
print 'A %s is fun.' % fun
print_plane = curry(print_fun, fun = 'plane')
print_plane()
--
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