Message328816
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? |
|
Date |
User |
Action |
Args |
2018-10-29 12:24:51 | vstinner | set | recipients:
+ vstinner, steven.daprano, docs@python, serhiy.storchaka |
2018-10-29 12:24:51 | vstinner | set | messageid: <1540815891.13.0.788709270274.issue35105@psf.upfronthosting.co.za> |
2018-10-29 12:24:51 | vstinner | link | issue35105 messages |
2018-10-29 12:24:50 | vstinner | create | |
|