add_command

Alex Martelli aleax at aleax.it
Mon May 13 04:34:38 EDT 2002


Gorny wrote:
        ...
>> Anyway, the special-case you need is something line (in Python 2.2,
>> or 2.1 with "from __future__ import nested_scopes"):
>>
>> def curry(func, *args, **kwds):
>>     def curried(): return func(*args, **kwds)
>>     return curried
>>
>> then you'll call something like
>>
>> status.add_command(label='wh00t',
>>   command=curry(self.func, *tuple_containing_args))
> 
> Allright, that'll do fine. I've read the corresponding PEP and it seems
> perfectly clear to me. But in the future (as of 2.3) I assume that you
> don't have to import anything and the nested_scopes are implemented
> within Python's core?

You don't have to import anything in Python 2.2, either - I was just
mentioning the import as needed in Python _2.1_, which is still out
and about (the current stable release of Zope needs 2.1.3, I think).

You can also curry in Python 1.5.2 should you need to -- it's just
a _little_ bit clunkier perhaps:

def curry(func, *args, **kwds):
    def curried(f=func, a=args, k=kwds):
        return apply(f, a, k)
    return curried

This works in any Python release (1.5.2 and later, at least) and,
apart from clarity or *tiny* performance differences, the only real
issue is that with this version you might accidentally call the
function built and returned from curry with an argument and thus
break the mechanism -- the 2.2 solution has no such risk (the
function built and returned from that version of curry takes no
arguments whatsoever, so trying to call it with an argument at
once raises an exception; the 1.5.2 solution uses "default
values of arguments" to compensate for the lack of lexically
nested scopes).


Alex




More information about the Python-list mailing list