On 28 March 2018 at 04:47, Paul Moore <p.f.mo...@gmail.com> wrote:
> On 27 March 2018 at 19:43, Ethan Furman <et...@stoneleaf.us> wrote:
>> On 03/27/2018 11:12 AM, Ivan Levkivskyi wrote:
>>>
>>> On 27 March 2018 at 18:19, Guido van Rossum wrote:
>>
>>>> Hm, so maybe we shouldn't touch lambda, but we can at least fix the scope
>>>> issues for comprehensions and genexprs.
>>>
>>>
>>> Removing the implicit function scope in comprehensions is something I
>>> wanted for long time.
>>> It would not only "fix" the scoping, but will also fix the yield inside
>>> comprehensions.
>>
>> Can we do it without leaking names?
>
> To me, that would be the ideal. I assume there are significant
> technical challenges, though, as otherwise I'd have thought that would
> have been the approach taken when Python 3 fixed the name leaking
> issue from Python 2.

It isn't avoiding the names leaking that's particularly challenging
(there are several viable ways to do that), it's making sure that
inner scopes can still see them. For example:

    lazy_and_eager = [((lambda: i), (lambda i=i: i)) for i in range(3)]
    for lazy, eager in lazy_and_eager:
        print(lazy(), eager())

    # prints:
    2 0
    2 1
    2 2

While it's *technically* feasible to hide "i" from the outer scope
without hiding it from inner scopes with a variable renaming based
approach, you end up having to design and document a separate lexical
scoping mechanism that's distinct from the one that functions already
use. I really didn't want to do that, so I proposed just using the
existing scoping mechanism instead, and while that has definitely had
its quirks, I'm still happy enough with it a decade later to call it a
successful approach.

Cheers,
Nick.

P.S. This is also a contributing factor to one of the aspects of the
sublocals proposal: disallowing closing over them means that a
renaming based approach *can* be used to keep them separate from
regular local variable names.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to