kw param question

kj no.email at please.post
Mon Aug 3 16:58:46 EDT 2009


In <mailman.4178.1249331189.8015.python-list at python.org> Albert Hopkins <marduk at letterboxes.org> writes:

>On Mon, 2009-08-03 at 19:59 +0000, kj wrote:
>> 
>> I want to write a decorator that, among other things, returns a
>> function that has one additional keyword parameter, say foo=None.
>> 
>> When I try
>> 
>> def my_decorator(f):
>>     # blah, blah
>>     def wrapper(*p, foo=None, **kw):
>>         x = f(*p, **kw)
>>         if (foo):
>>             # blah, blah
>>         else
>>             # blah blah
>>     return wrapper 
>> 
>> ...i get a syntax error where it says "foo=None".  I get similar
>> errors with everything else I've tried.
>> 

>Not exactly sure what you're trying to do..

Yeah, I wasn't too clear.  I figured out how to do what I wanted
to do:

def my_decorator(f):
    # blah, blah
    def wrapper(*p, **kw):
        foo = kw.pop('force', None)
        x = f(*p, **kw)
        if (foo):
            # blah, blah
        else
            # blah blah
    return wrapper 

Now the definitions of the original functions do not include the
foo=None argument, but "actual" functions (i.e. the ones generated
by the decorator) all accept the optional foo parameter.  The only
remaining problem is how to document this...  I don't see how pydoc
could possibly figure this one out.  I guess this is sufficient
argument to abandon this idea.  Bummer.

kynn



More information about the Python-list mailing list