Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103087
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Python keyword args can be any string |
| Date | 2016-02-18 01:31 -0500 |
| Message-ID | <mailman.233.1455777087.22075.python-list@python.org> (permalink) |
| References | <56c559c2$0$2916$c3e8da3$76491128@news.astraweb.com> <857fi236ek.fsf@benfinney.id.au> |
On 2/18/2016 12:59 AM, Ben Finney wrote:
> Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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.
The was discussed on pydev some years ago. Guido says leave as is. This
and similar cases of allowing non-identifier is string-dicts does not
seem to cause a problem and apparently has a few rare legitimate usages.
> 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.
>
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python keyword args can be any string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-02-18 16:42 +1100
Re: Python keyword args can be any string Chris Angelico <rosuav@gmail.com> - 2016-02-18 16:57 +1100
Re: Python keyword args can be any string Ben Finney <ben+python@benfinney.id.au> - 2016-02-18 16:59 +1100
Re: Python keyword args can be any string Terry Reedy <tjreedy@udel.edu> - 2016-02-18 01:31 -0500
Re: Python keyword args can be any string Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-02-18 07:55 +0000
Re: Python keyword args can be any string Steven D'Aprano <steve@pearwood.info> - 2016-02-19 11:03 +1100
Re: Python keyword args can be any string Ben Finney <ben+python@benfinney.id.au> - 2016-02-19 11:58 +1100
Re: Python keyword args can be any string Rustom Mody <rustompmody@gmail.com> - 2016-02-19 03:41 -0800
csiph-web