[Python-ideas] Debugging: some problems and possible solutions

Jonathan Fine jfine2358 at gmail.com
Wed Oct 3 15:52:18 EDT 2018


Hi Wolfram

You tried
>    def f(a):
> ..   print(a)
>    f(**{"a":2})
> 2
>    f(**{"a+1":2})
> Traceback (most recent call last):
>   File "python", line 1, in <module>
> TypeError: f() got an unexpected keyword argument 'a+1'

This is exactly what I would have expected. Please consider the following:

>>> def f(a): pass
>>> f(**dict(b=1))
TypeError: f() got an unexpected keyword argument 'b'

> Does CPython count as "other python implementation"?

Yes and No. Both are half correct. CPython is the reference
implementation. Please also consider

>>> def f(a, **kwargs): pass
>>> f(a=1, **{'a+1': 2})

>>> f(a=1, **{(0, 1): 2})
TypeError: f() keywords must be strings

So far as I know, that a keyword be a string is the only constraint,
at least in CPython. For example

>>> def f(a, **kwargs): pass
>>> f(a=1, **{'': 2})
>>> f(a=1, **{'def': 2})

So I think Anders proposal works in CPython. I think you forgot the
**kwargs in the parameters to f.

best regards

Jonathan


More information about the Python-ideas mailing list