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 05:00:25 CET 2012


Emile van Sebille wrote:
> brucegoodstein at gmail.com 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~


More information about the Python-list mailing list