PEP 572: clarify Appendix C example (#732)

* PEP 572:  clarify Appendix C example

Make very clear in the example that the function local remains unbound until the caller executes the genexp.
This commit is contained in:
Tim Peters 2018-07-10 17:17:34 -05:00 committed by GitHub
parent 801bfd1b0f
commit 0919149969
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 3 deletions

View File

@ -1201,15 +1201,22 @@ closures"). Example::
a = 42
def f():
# `a` is local to `f`
# `a` is local to `f`, but remains unbound
# until the caller executes this genexp:
yield ((a := i) for i in range(3))
yield lambda: a + 100
print("done")
try:
print(f"`a` is bound to {a}")
assert False
except UnboundLocalError:
print("`a` is not yet bound")
Then::
>>> results = list(f()) # [genexp, lambda]
done
`a` is not yet bound
# 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))
@ -1236,8 +1243,6 @@ Copyright
This document has been placed in the public domain.
..
Local Variables:
mode: indented-text