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

E

Ethan Furman

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~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top