Python keyword args can be any string

Ben Finney ben+python at
Thu Feb 18 00:59:47 EST 2016

Steven D'Aprano <steve+comp.lang.python at> writes:

> Today I learned that **kwargs style keyword arguments can be any string:
> py> def test(**kw):
> ...     print(kw)
> ... 
> py> kwargs = {'abc-def': 42, '': 23, '---': 999, '123': 17}
> py> test(**kwargs)
> {'': 23, '123': 17, '---': 999, 'abc-def': 42}
> Bug or feature?

Incidental feature, I think.

If the caller is deliberately unpacking a dict, it's on them to ensure
the keys are valid identifiers or wear the consequences.

If the function cares so little about the keys in its kwargs that (as in
your example) any string will do for each key, then it's a consenting
actor IMO.

The times when it's a problem – i.e. that the function is trying to use
items from that dictionary as name bindings – the errors will be raised
to the caller that unpacked that mapping. That's where the errors belong.

 \      “There's a fine line between fishing and standing on the shore |
  `\                            looking like an idiot.” —Steven Wright |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list