[Python-ideas] A "local" pseudo-function
Chris Angelico
rosuav at gmail.com
Sat Apr 28 23:36:31 EDT 2018
On Sun, Apr 29, 2018 at 12:50 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, Apr 28, 2018 at 12:16:16PM -0500, Tim Peters wrote:
>> [Steven D'Aprano <steve at pearwood.info>]
>> > Chris' PEP 572 started off with the concept that binding expressions
>> > would create a "sub-local" scope, below function locals. After some
>> > debate on Python-Ideas, Chris, Nick and Guido took the discussion off
>> > list and decided to drop the sub-local scope idea as confusing and hard
>> > to implement.
>>
>> Enormously harder to implement than binding expressions, and the
>> latter (to my eyes) capture many high-value use cases "good enough".
>
> And yet you're suggesting an alternative which is harder and more
> confusing. What's the motivation here for re-introducing sublocal
> scopes, if they're hard to do and locals are "good enough"?
>
> That's not a rhetorical question: why have you suggested this sublocal
> scoping idea? PEP 572 stopped talking about sublocals back in revision 2
> or so, and as far as I can see, *not a single objection* since has
> been that the variables weren't sublocal.
No objections, per se, but I do know a number of people were saddened
at their loss. And it's not like eliminating sublocals solved problems
without creating more; it's just a different set of trade-offs.
Sublocals have their value.
>> It was also the case that nesting scopes _at all_ was very
>> controversial in Python's earliest years, and Guido resisted it
>> mightily (with my full support). The only scopes at first were
>> function-local, module-global, and builtin, and while functions could
>> _textually_ nest, they had no access to enclosing local scopes.
>
> While I started off with Python 1.5, I wasn't part of the discussions
> about nested scopes. But I'm astonished that you say that nested scopes
> were controversial. *Closures* I would completely believe, but mere
> lexical scoping? Astonishing.
I'm not sure how you can distinguish them:
> This behaviour in Python 1.5 made functions MUCH less useful:
>
>
>>>> def outer():
> ... x = 1
> ... def inner():
> ... return x
> ... return inner()
> ...
>>>> outer()
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "<stdin>", line 5, in outer
> File "<stdin>", line 4, in inner
> NameError: x
>
>
> I think it is fair to say that inner functions in Python 1.5 were
> crippled to the point of uselessness.
What you expect here is lexical scope, yes. But if you have lexical
scope with no closures, the inner function can ONLY be used while its
calling function is still running. What would happen if you returned
'inner' uncalled, and then called the result? How would it resolve the
name 'x'? I can't even begin to imagine what lexical scope would do in
the absence of closures. At least, not with first-class functions. If
functions aren't first-class objects, it's much easier, and a nested
function serves as a refactored block of code. But then you can't even
pass a nested function to a higher order function.
ChrisA
More information about the Python-ideas
mailing list