Is there a simpler way to modify all arguments in a function before using the arguments?

Ethan Furman ethan at stoneleaf.us
Fri Nov 16 16:30:34 EST 2012


bruceg113355 at gmail.com wrote:
> On Thursday, November 15, 2012 11:16:08 PM UTC-5, Ethan Furman wrote:
>> Emile van Sebille wrote:
>>
>>
>>>> Using a decorator works when named arguments are not used. When named 
>>>> arguments are used, unexpected keyword error is reported. Is there a 
>>>> simple fix?
>>> Extend def wrapper(*args) to handle *kwargs as well
>>> Emile
>>>> Code:
>>>> -----
>>>> from functools import wraps
>>>> def fix_args(fn):
>>>>     @wraps(fn)
>>>>     def wrapper(*args):
>> so this line ^ becomes
>>
>>         def wrapper(*args, **kwargs):
>>
>>>>         args = (arg.replace('_', '') for arg in args)
>> and add a line
>>
>>             for k, v in kwargs:
>>
>>                 kwargs[k] = v.replace('_', '')
>>
>>>>         return fn(*args)
>> and this line ^ becomes
>>
>>             return fn(*args, **kwargs)
>>
>>>>     return wrapper
>>
>>
>> ~Ethan~
> 
> 
> Ethan,
> 
> I tried you code suggestions but got errors.

Right, my 'for k, v in kwargs' should have been 'for k, v in kwargs.items()'

Glad you were able to make it work!

~Ethan~



More information about the Python-list mailing list