Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103159
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ben Finney <ben+python@benfinney.id.au> |
| Newsgroups | comp.lang.python |
| Subject | Re: Python keyword args can be any string |
| Date | Fri, 19 Feb 2016 11:58:41 +1100 |
| Lines | 57 |
| 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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | news.uni-berlin.de LCSHVMUW2SRUJxWeQxeK4Q28A9a+G65oWCXnFADkTmAw== |
| Cancel-Lock | sha1:ZN4Y8orIKDhD43VaCmjWzoUXXRo= |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'before.': 0.09; 'kwargs': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'typeerror:': 0.09; 'unexpected': 0.09; 'bug': 0.10; 'python': 0.10; 'def': 0.13; 'argument': 0.15; 'skip:f 30': 0.15; '"-"': 0.16; '(name,': 0.16; 'exposes': 0.16; 'imo.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'skip:{ 20': 0.18; 'test.': 0.18; '>>>': 0.20; 'all,': 0.20; 'student': 0.20; '"",': 0.22; 'arguments': 0.22; 'mind.': 0.22; 'names.': 0.22; 'pass': 0.22; 'seems': 0.23; 'passing': 0.23; 'specified': 0.23; '(most': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; '**kwargs):': 0.29; '13,': 0.29; 'value)': 0.29; 'another': 0.32; 'point': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'know.': 0.34; 'file': 0.34; 'should': 0.36; 'instead': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'wanted': 0.37; 'starting': 0.37; 'test': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'provide': 0.61; 'improved': 0.63; 'skip:\xe2 10': 0.70; '8bit%:40': 0.72; '8bit%:43': 0.72; 'asap': 0.72; '_o__)': 0.84; 'mencken': 0.84; 'received:125': 0.84; 'subject:any': 0.84; '\xe2\x80\x94henry': 0.84; 'colleague': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | jigong.madmonks.org |
| X-Public-Key-ID | 0xAC128405 |
| X-Public-Key-Fingerprint | 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 |
| X-Public-Key-URL | http://www.benfinney.id.au/contact/bfinney-pubkey.asc |
| X-Post-From | Ben Finney <bignose+hates-spam@benfinney.id.au> |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21rc2 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:103159 |
Show key headers only | View raw
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