[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part) (original) (raw)
Steven D'Aprano steve at pearwood.info
Sun Jun 24 01:56:47 EDT 2018
- Previous message (by thread): [Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)
- Next message (by thread): [Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, Jun 24, 2018 at 02:33:59PM +1000, Nick Coghlan wrote:
Given that PEP 572 is proposing implicit comprehension state export,
"Implicit" and "explicit" are two terms which often get misused to mean "I don't like it" and "I do like it".
Making the intentional choice to use an assignment expression is not really "implicit" in any meaningful sense. One might as well complain that "import this" implicitly creates a local variable "this". Well, yes, it does, in a very loose sense, but that's what imports are defined as do and it is the whole purpose for making them.
If PEP 572's proposal goes ahead, the behaviour of assignment expressions will be defined as creating assignments in the local scope rather than the sublocal comprehension scope. To call that "implicit" is rather like saying that regular assignment is implicit.
though, then I think it's important to make the case that seeing the proposed semantics as intuitive is only going to be the case for folks that have used Python 2 style comprehensions extensively - anyone that's never encountered the old state-leaking behaviour for iteration variables is going to be surprised when assignment expressions ignore the existence of the comprehension scope (even though the iteration variable pays attention to it).
You are making the assumption that most people are even aware of "comprehension scope". I don't think that is the case.
In my experience, scoping in Python is still typically seen as the LGB rule (locals/globals/builtins). See for example this StackOverflow post from 2016:
https://stackoverflow.com/questions/37211910/override-lgb-scope-rule
Sometimes people remember the E/N (enclosing function/nonlocal) part. Hardly anyone remembers the C (class) part unless they are actively thinking in terms of code running inside a class definition, and even if they do, they typically aren't sure of exactly how it interacts with the rest.
And I predict that even fewer think of comprehensions as a separate scope, except by ommission: they don't think about the scope of the loop variable until it bites them.
But as Tim Peters has previously discussed, the loop variable is special, and is especially prone to accidental shadowing. That won't be the case for assignment expressions. If there's shadowing going on, it will be deliberate.
Aside: I've said before that I'm not a fan of sublocal comprehension scoping, since I personally found it helpful on occasion for the loop variable to be visible outside of the comprehension. But given that the only experience most people apparently had with comprehension scoping was to be bitten by it, I grudgingly accept that encapsulating the loop variable was the right decision to make, even if it personally inconvenienced me more than it saved me.
Nor was I the only one: others have been bitten by the change to comprehension scope, see for example:
https://www.reddit.com/r/Python/comments/425qmb/strange_python_27_34_difference/
There is no consensus that the change to comprehensions was a good thing or justified.
The bottom line is that I don't think people will be surprised by assignment expression scope being local instead of sublocal. Rather I expect that they won't even think about it, until they do, and then whatever behaviour we pick, we'll annoy somebody.
-- Steve
- Previous message (by thread): [Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)
- Next message (by thread): [Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]