PEP 572: Add an appendix to clarify that there's no magical scoping (#714)

This commit is contained in:
Chris Angelico 2018-07-10 05:24:47 +10:00 committed by GitHub
parent 7d51890d81
commit 096ef3b37f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 0 deletions

View File

@ -1170,6 +1170,37 @@ Finally, let's nest two comprehensions.
print(TARGET) print(TARGET)
Appendix C: No Changes to Scope Semantics
=========================================
Because it has been a point of confusion, note that nothing about Python's
scoping semantics is changed. Function-local scopes continue to be resolved
at compile time, and to have indefinite temporal extent at run time ("full
closures"). Example::
a = 42
def f():
# `a` is local to `f`
yield ((a := i) for i in range(3))
yield lambda: a + 100
print("done")
Then::
>>> results = list(f()) # [genexp, lambda]
done
# The execution frame for f no longer exists in CPython,
# but f's locals live so long as they can still be referenced.
>>> list(map(type, results))
[<class 'generator'>, <class 'function'>]
>>> list(results[0])
[0, 1, 2]
>>> results[1]()
102
>>> a
42
References References
========== ==========