Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: Python keyword args can be any string Date: Thu, 18 Feb 2016 16:59:47 +1100 Lines: 34 Message-ID: References: <56c559c2$0$2916$c3e8da3$76491128@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de xjhmjGQLGf9ZNvA2FzjfagCCqtZq27Kk1GgWnDCYnY7w== Cancel-Lock: sha1:5+2J7EA9TpjUVu3Ri0fPIOassv4= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'caller': 0.07; 'bindings': 0.09; 'incidental': 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; 'unpacking': 0.09; 'bug': 0.10; 'def': 0.13; "'':": 0.16; '23,': 0.16; '42,': 0.16; 'cares': 0.16; 'example)': 0.16; 'feature?': 0.16; 'fishing': 0.16; 'identifiers': 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; 'string:': 0.16; 'unpacked': 0.16; "{'':": 0.16; 'string': 0.17; 'arguments': 0.22; 'keys': 0.22; 'trying': 0.22; 'errors': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'fine': 0.28; 'dictionary': 0.29; 'feature,': 0.29; 'key,': 0.29; 'wright': 0.29; 'problem': 0.33; "d'aprano": 0.33; 'raised': 0.33; 'steven': 0.33; 'i.e.': 0.35; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'times': 0.63; 'between': 0.65; 'today': 0.65; '\xe2\x80\x93': 0.72; '_o__)': 0.84; 'dict,': 0.84; 'received:125': 0.84; 'shore': 0.84; 'standing': 0.84; 'subject:any': 0.84 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103085 Steven D'Aprano 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