[Python-ideas] PEP 572 version 2: Statement-Local Name Bindings
davidfstr at gmail.com
Sat Mar 17 12:10:32 EDT 2018
I mean approximately local to one line of source code. Perhaps the
unpopular opinion based on your reaction. :)
More specifically, for a simple statement (with no trailing colon), there
is one scope enclosing everything in the statement. For a compound
statement, composed of multiple clauses, where each clause has a header
(ending with a colon) and a suite, there are N non-overlapping scopes, one
scope for each of the N clause headers. The scope is limited to the header
only and does not include the suite.
In considering a 'for' loop, I'd advocate for keeping the scope of the
expression_list separate from the target_list, since I can't think of a
reasonable case where the target_list would want to reference something
from the expression_list. So the following code would have a NameError for
magic_index in the target_list:
> # NameError: name 'magic index' is not defined
> for my_array[magic_index] in list_of_lists[(f(...) as magic_index)]:
That's pretty bizarre code, using a fixed index of an array as an iteration
variable. The only other type of 'for' loop target that might try to use a
named expression from the expression_list is a slice expression, which
would be even more bizarre code. Best to make bizarre cases into errors.
David Foster | Seattle, WA, USA
On Sat, Mar 17, 2018 at 12:13 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Mar 17, 2018 at 5:49 PM, David Foster <davidfstr at gmail.com> wrote:
> > (3a) With a header-limited scope (in proposal #1 above), I advocate that
> > named expression should NOT be able to shadow other variables, giving a
> > SyntaxError. I can't think of a reasonable reason why such shadowing
> > be allowed, and preventing shadowing may eliminate unintentional errors.
> Header-limited scope is hard to define. Do you mean expression-local?
> (Also hard to define.) Do you mean local to one line of source code?
> Definitely not. And what happens with a 'for' loop - part of its
> header gets run after each loop iteration but still kinda references
> stuff that was done once-only before the loop started.
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas