This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Clarify that writing to locals() inside a class body is supported
Type: Stage:
Components: Documentation Versions: Python 3.6, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, martin.panter, steven.daprano
Priority: normal Keywords:

Created on 2016-06-16 17:35 by steven.daprano, last changed 2022-04-11 14:58 by admin.

Messages (2)
msg268682 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-06-16 17:35
The docs for locals() warn not to write to the dict returned, as it may not have the intended effect of modifying the actual variables seen by the interpreter.

https://docs.python.org/3/library/functions.html#locals

But as I understanding it, using locals() inside a class body is intentionally supported:

    class K:
        locals()['x'] = 1
    
    assert K.x == 1

is not just an accident of implementation, but the intended behaviour and a language guarantee.
msg268704 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-06-16 23:58
I think my proposed patch for Issue 17546 addresses this. That patch was also written to address Issue 17960.
History
Date User Action Args
2022-04-11 14:58:32adminsetgithub: 71522
2016-06-16 23:58:04martin.pantersetnosy: + martin.panter
messages: + msg268704
2016-06-16 17:35:01steven.dapranocreate