OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Arbitrary non-identifier string keys when using **kwargs


*locals *and *globals* are documented as dictionaries (for example exec's
documentation states that " If only *globals* is provided, it must be a
dictionary") but __dict__ is described as " [a] dictionary or other mapping
object".


On Sun, 7 Oct 2018 at 19:38, Chris Barker via Python-Dev <
python-dev at python.org> wrote:

> On Fri, Oct 5, 2018 at 3:01 PM Brett Cannon <brett at python.org> wrote:
>
>> I'm also fine with saying that keys in **kwargs that are not proper
>> identifiers is an implementation detail.
>>
>
> It's not just **kwargs -- you can also use arbitrary names with setattr()
> / getattr() :
>
> In [6]: setattr(foo, "4 not an identifier", "this works")
>
> In [7]: getattr(foo, "4 not an identifier")
> Out[7]: 'this works'
>
> Which brings up a question I've had for years -- is the fact that cPython
> uses a regular old dict for namespaces (and **kwargs) part of the language
> spec, or an implementation detail?
>
> I would say that for the get/setattr() example, it is kinda handy when you
> want to use a class instance to model some external data structure that may
> have different identifier rules. Though I tend to think that's mingling
> data and code too much.
>
> -CHB
>
>
>>
>> On Thu, 4 Oct 2018 at 02:20, Serhiy Storchaka <storchaka at gmail.com>
>> wrote:
>>
>>> 04.10.18 11:56, Steven D'Aprano ????:
>>> > While keyword arguments have to be identifiers, using **kwargs allows
>>> > arbitrary strings which aren't identifiers:
>>> >
>>> > py> def spam(**kwargs):
>>> > ....     print(kwargs)
>>> > ....
>>> > py> spam(**{"something arbitrary": 1, '\n': 2})
>>> > {'something arbitrary': 1, '\n': 2}
>>> >
>>> >
>>> > There is some discussion on Python-Ideas on whether or not that
>>> > behaviour ought to be considered a language feature, an accident of
>>> > implementation, or a bug.
>>> >
>>> > Can we get some guidence on this please?
>>>
>>> This is an implementation detail. Currently CPython doesn't ensure that
>>> keyword argument names are identifiers for performance reasons. But this
>>> can be changed in future versions or in other implementations.
>>>
>>> _______________________________________________
>>> Python-Dev mailing list
>>> Python-Dev at python.org
>>> https://mail.python.org/mailman/listinfo/python-dev
>>> Unsubscribe:
>>> https://mail.python.org/mailman/options/python-dev/brett%40python.org
>>>
>> _______________________________________________
>> Python-Dev mailing list
>> Python-Dev at python.org
>> https://mail.python.org/mailman/listinfo/python-dev
>> Unsubscribe:
>> https://mail.python.org/mailman/options/python-dev/chris.barker%40noaa.gov
>>
>
>
> --
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> Chris.Barker at noaa.gov
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/jmcs%40jsantos.eu
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20181007/abe7dfd6/attachment.html>