New submission from STINNER Victor <vstin...@redhat.com>:

The Python 3 language has a strict definition for an identifier:
https://docs.python.org/dev/reference/lexical_analysis.html#identifiers

... but in practice, it's possible to create "invalid" identifiers using 
setattr().

Example with PyPy:

$ pypy
Python 2.7.13 (0e7ea4fe15e82d5124e805e2e4a37cae1a402d4b, Apr 12 2018, 14:50:12)
>>>> class A: pass
>>>> 
>>>> a=A()
>>>> setattr(a, "1", 2)
>>>> getattr(a, "1")
2

>>>> vars(a)
{'1': 2}
>>>> a.__dict__
{'1': 2}

>>>> a.1
  File "<stdin>", line 1
    a.1
    ^
SyntaxError: invalid syntax


The exact definition of "identifiers" is a common question. Recent examples:

* bpo-25205
* [Python-Dev] Arbitrary non-identifier string keys when using **kwargs
  https://mail.python.org/pipermail/python-dev/2018-October/155435.html

It would be nice to document the answer. Maybe in the Langage Specification, 
maybe in the setattr() documentation, maybe in a FAQ, maybe everywhere?

----------
assignee: docs@python
components: Documentation
messages: 328816
nosy: docs@python, serhiy.storchaka, steven.daprano, vstinner
priority: normal
severity: normal
status: open
title: Document that CPython accepts "invalid" identifiers
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue35105>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to