Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103159
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Python keyword args can be any string |
| Date | 2016-02-19 11:58 +1100 |
| Message-ID | <mailman.28.1455843544.2289.python-list@python.org> (permalink) |
| References | <56c559c2$0$2916$c3e8da3$76491128@news.astraweb.com> <mailman.237.1455782186.22075.python-list@python.org> <56c65be5$0$1590$c3e8da3$5496439d@news.astraweb.com> |
Steven D'Aprano <steve@pearwood.info> writes:
> A work colleague wanted to pass an argument starting with "-" to a
> function.
>
> Apparently he didn't have a specific argument in mind. He just wanted
> to test the function to breaking point by passing invalid argument
> names.
That seems a reasonable test.
>>> kwargs = {
... 'spam': 13,
... 'eggs': 17,
... '-beans': 11,
... }
>>> foo(**kwargs) # Should this fail?
It exposes a design smell; by capturing ‘**kwargs’, a function has
deiberately not specified which keyword arguments it will accept.
>>> def foo_captures_kwargs(*args, **kwargs):
... for (name, value) in kwargs.items():
... print("Got argument {name}={value!r}".format(
... name=name, value=value))
...
>>> foo_captures_kwargs(**kwargs)
Got argument eggs=17
Got argument spam=13
Got argument -beans=11
Still not a bug in Python IMO. It may be a bug in the program; the
design of the function doesn't provide any way to know.
Perhaps the design can be improved by not using ‘**kwargs’ at all, and
instead using a specific set of keyword-only arguments.
>>> def foo_names_every_argument(*, spam="Lorem", eggs="ipsum"):
... print("Got argument {name}={value!r}".format(
... name='spam', value=spam))
... print("Got argument {name}={value!r}".format(
... name='eggs', value=eggs))
...
>>> foo_names_every_argument(**kwargs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo_names_every_argument() got an unexpected keyword argument '-beans'
This is IMO another good reason to migrate ASAP to Python 3; better
design is easier that before.
--
\ “Judge: A law student who marks his own papers.” —Henry L. |
`\ Mencken |
_o__) |
Ben Finney
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