Tuesday, 15 May 2012

python - Pass dict with non string keywords to function in kwargs -



python - Pass dict with non string keywords to function in kwargs -

i work library has function signature f(*args, **kwargs). need pass python dict in kwargs argument, dict contains not strings in keywords

f(**{1: 2, 3: 4}) traceback (most recent phone call last): file "<console>", line 1, in <module> typeerror: f() keywords must strings

how can around without editing function?

non-string keyword arguments not allowed, there no general solution problem. specific illustration can fixed converting keys of dict strings:

>>> kwargs = {1: 2, 3: 4} >>> f(**{str(k): v k, v in kwargs.items()})

python function python-3.x arguments

No comments:

Post a Comment