[Python-Dev] Arbitrary non-identifier string keys when using **kwargs (original) (raw)
Samuele Pedroni samuele.pedroni at gmail.com
Fri Oct 12 03:08:32 EDT 2018
- Previous message (by thread): [Python-Dev] Arbitrary non-identifier string keys when using **kwargs
- Next message (by thread): [Python-Dev] Summary of Python tracker Issues
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Thu, Oct 4, 2018 at 10:58 AM Steven D'Aprano <steve at pearwood.info> wrote:
While keyword arguments have to be identifiers, using **kwargs allows arbitrary strings which aren't identifiers:
py> def spam(**kwargs): ... print(kwargs) ... py> spam(**{"something arbitrary": 1, '\n': 2}) {'something arbitrary': 1, '\n': 2}
There is some discussion on Python-Ideas on whether or not that behaviour ought to be considered a language feature, an accident of implementation, or a bug. I would expect this to be costly/annoying for implementations to enforce, doing it at call time is probably too late to be efficient, it would need help from dicts themselves or even strings.
A hack that currently works because of this is with dict itself:
d = {'a-1': 1, 'a-2': 2, 'a-3': 3} d1 = dict(d, **{'a-2': -2, 'a-1': -1}) d1 is d False d1 {'a-1': -1, 'a-2': -2, 'a-3': 3} -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20181012/eecd4783/attachment.html>
- Previous message (by thread): [Python-Dev] Arbitrary non-identifier string keys when using **kwargs
- Next message (by thread): [Python-Dev] Summary of Python tracker Issues
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]