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

Peter Otten __peter__ at web.de
Sat Nov 10 04:09:08 EST 2012


Miki Tebeka wrote:

>> Is there a simpler way to modify all arguments in a function before using
>> the arguments?
> You can use a decorator:
> 
> from functools import wraps
> 
> def fix_args(fn):
>     @wraps(fn)
>     def wrapper(*args):
>         args = (arg.replace('_', '') for arg in args)
>         return fn(*args)
> 
>     return wrapper
> 
> @fix_args
> def foo(x, y):
>     print(x)
>     print(y)

I was tempted to post that myself, but he said /simpler/ ;)




More information about the Python-list mailing list