PEP 572: Add a section detailing how SLNBs are different from regular

This commit is contained in:
Chris Angelico 2018-03-03 03:00:05 +11:00
parent 315026e1e1
commit d5298fc7be
1 changed files with 19 additions and 0 deletions

View File

@ -92,6 +92,25 @@ Some of these examples should be considered *bad code* and rejected by code
review and/or linters; they are not, however, illegal. review and/or linters; they are not, however, illegal.
Differences from regular assignment statements
----------------------------------------------
Using ``(EXPR as NAME)`` is similar to ``NAME = EXPR``, but has a number of
important distinctions.
* Assignment is a statement; an SLNB is an expression whose value is the same
as the object bound to the new name.
* SLNBs disappear at the end of their enclosing statement, at which point the
name again refers to whatever it previously would have. SLNBs can thus
shadow other names without conflict (although deliberately doing so will
often be a sign of bad code).
* SLNBs cannot be closed over by nested functions, and are completely ignored
for this purpose.
* SLNBs do not appear in ``locals()`` or ``globals()``.
* An SLNB cannot be the target of any form of assignment, including augmented.
Attempting to do so will remove the SLNB and assign to the fully-scoped name.
Example usage Example usage
============= =============