[Python-ideas] Explicit variable capture list

Nick Coghlan ncoghlan at gmail.com
Sun Jan 24 21:31:38 EST 2016


On 25 January 2016 at 05:57, Brett Cannon <brett at python.org> wrote:
>
> On Sun, Jan 24, 2016, 04:55 Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>> On 24 January 2016 at 15:16, Guido van Rossum <guido at python.org> wrote:
>> > I wonder if kids today aren't too much in love with local function
>> > definitions. :-) There's a reason why executor.submit() takes a
>> > function *and arguments*. If you move the function out of the for loop
>> > and pass the url as a parameter to submit(), problem solved, and you
>> > waste fewer resources on function objects and cells to hold nonlocals.
>>
>> Aye, that's how the current example code in the docs handles it -
>> there's an up front definition of the page loading function, and then
>> the submission to the executor is with a dict comprehension.
>>
>> The only thing "wrong" with it is that when reading the code, the
>> potentially single-use function is introduced first without any
>> context, and it's only later that you get to see what it's for.
>
> So the doics just need an added comment to help explain it. Want to file an
> issue for that?

There's nothing to comment on given the Python semantics we have today
- what's there is a sensible way to write that code, and the design
FAQ covers why the inline closure approach wouldn't work.

As noted, I suspect the only reason the topic keeps coming up is the
niggling sense that the closure based approach "should" work, and the
fact that it doesn't is a case where underlying technical details that
we generally aim to let people gloss over make themselves apparent.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list