Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103085

Re: Python keyword args can be any string

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Python keyword args can be any string
Date 2016-02-18 16:59 +1100
Message-ID <mailman.232.1455775197.22075.python-list@python.org> (permalink)
References <56c559c2$0$2916$c3e8da3$76491128@news.astraweb.com>

Show all headers | View raw


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.

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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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