PEP 572: Add an appendix to clarify that there's no magical scoping (#714)
This commit is contained in:
parent
7d51890d81
commit
096ef3b37f
31
pep-0572.rst
31
pep-0572.rst
|
@ -1170,6 +1170,37 @@ Finally, let's nest two comprehensions.
|
|||
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
|
||||
==========
|
||||
|
||||
|
|
Loading…
Reference in New Issue