[Python-ideas] Explicit variable capture list

Jim J. Jewett jimjjewett at gmail.com
Thu Jan 28 14:51:17 EST 2016



On Wed Jan 27 15:49:12 EST 2016, Andrew Barnert wrote:

> both C# and Ruby made breaking changes from the Python behavior
> to the Swift behavior, because they couldn't find any legitimate code
> that would be broken by that change. And there have been few if any
> complaints since. If we really are considering adding something like
> "for let", we should seriously consider whether anyone would ever have
> a good reason to use "for" instead of "for let". If not, just change
> "for" instead.

The first few times I saw this, I figured Python had a stronger (and
longer) backwards compatibility guarantee.

But now that I consider the actual breakage, I'm not so sure...

    >>> for i in range(10):
            print (i)
            i=i+3
            print(i)

i is explicitly changed, but it doesn't affect the flow control --
it gets reset to the next sequence item as if nothing had happened.

It would break things to hide the final value of i after the loop
is over, but that isn't needed.

I think the only way it even *could* matter is if the loop variable is
captured in a closure each time through the loop.  What would it
look like for the current behavior to be intentional?

    >>> for cache in (4, 5, 6, {}):
            def f():
                cache['haha!'] = "I know only the last will really get used!"
            funcs.append(f)
            

-jJ

--

If there are still threading problems with my replies, please
email me with details, so that I can try to resolve them.  -jJ


More information about the Python-ideas mailing list