Author vstinner
Recipients docs@python, serhiy.storchaka, steven.daprano, vstinner
Date 2018-10-29.12:24:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1540815891.13.0.788709270274.issue35105@psf.upfronthosting.co.za>
In-reply-to
Content
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?
History
Date User Action Args
2018-10-29 12:24:51vstinnersetrecipients: + vstinner, steven.daprano, docs@python, serhiy.storchaka
2018-10-29 12:24:51vstinnersetmessageid: <1540815891.13.0.788709270274.issue35105@psf.upfronthosting.co.za>
2018-10-29 12:24:51vstinnerlinkissue35105 messages
2018-10-29 12:24:50vstinnercreate