bruceg113...@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~
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to