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

2018-10-07 Thread Chris Barker via Python-Dev
On Fri, Oct 5, 2018 at 3:01 PM Brett Cannon  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  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@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@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.bar...@noaa.gov
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


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

2018-10-07 Thread João Santos
*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@python.org> wrote:

> On Fri, Oct 5, 2018 at 3:01 PM Brett Cannon  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 
>> 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@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@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.bar...@noaa.gov
> ___
> Python-Dev mailing list
> Python-Dev@python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/jmcs%40jsantos.eu
>
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


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

2018-10-07 Thread Simon Cross
I would consider it a feature. My reasoning is that the restriction on
what can be used as an identifier is a syntax restriction, not a
general restriction on what attributes or names can be.
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


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

2018-10-07 Thread Chris Barker via Python-Dev
On Sun, Oct 7, 2018 at 11:42 AM João Santos  wrote:

> *locals *and *globals* are documented as dictionaries (for example exec's
> documentation states that " If only *globals* is provided, it must be a
> dictionary")
>

well, that is specifically about exec() -- it may or may not apply to
everywhere nameapaces are used in the interpreter...


> but __dict__ is described as " [a] dictionary or other mapping object".
>

exactly.

-CHB

On Sun, 7 Oct 2018 at 19:38, Chris Barker via Python-Dev <
> python-dev@python.org> wrote:
>
>> On Fri, Oct 5, 2018 at 3:01 PM Brett Cannon  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 
>>> 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@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@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.bar...@noaa.gov
>> ___
>> Python-Dev mailing list
>> Python-Dev@python.org
>> https://mail.python.org/mailman/listinfo/python-dev
>> Unsubscribe:
>> https://mail.python.org/mailman/options/python-dev/jmcs%40jsantos.eu
>>
>

-- 

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.bar...@noaa.gov
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


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

2018-10-07 Thread Terry Reedy

On 10/7/2018 1:34 PM, Chris Barker via Python-Dev wrote:
On Fri, Oct 5, 2018 at 3:01 PM Brett Cannon > 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'


When this behavior of set/getattr was discussed a decade or so ago, 
Guido said not to disable it, but I believe he said it should not be 
considered a language feature.  There are other situations where CPython 
is 'looser' than the spec.


--
Terry Jan Reedy

___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


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

2018-10-07 Thread Stephen J. Turnbull
Terry Reedy writes:

 > When this behavior of set/getattr was discussed a decade or so ago,
 > Guido said not to disable it, but I believe he said it should not
 > be considered a language feature.  There are other situations where
 > CPython is 'looser' than the spec.

I'm pretty sure that all of these mappings that create namespaces are
*not* specified to be dicts.  globals() and locals() need to return
*something*, and dict is the only builtin mapping.  On the other hand,
it is explicit that changes to these dicts need not be reflected in the
namespace.

Note that the namespace defined by a class is *not* a dict, it's a
union (it may be a dict, or it may be slots):

>>> class Foo:
...  __slots__ = ('a')
...  def __init__(self, x):
...   self.a = x
...   self.b = (x,)
... 
>>> fu = Foo(1)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 6, in __init__
AttributeError: 'Foo' object has no attribute 'c'
>>> class Foo:
...  __slots__ = ('a')
...  def __init__(self, x):
...   self.a = x
... 
>>> fu = Foo(1)
>>> print(fu.a)
1
>>> print(fu.__dict__)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'Foo' object has no attribute '__dict__'

This is a useful optimization if there are a lot of Foo objects, and
is somewhat faster.  As I understand it, while nobody has yet found a
reason to optimize other namespaces in such a way (or extend them in
some way, for that matter), the option is intentionally open.

Steve
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com