[Python-ideas] Explicit variable capture list

Yury Selivanov yselivanov.ml at gmail.com
Wed Jan 20 14:05:17 EST 2016


Nick,

On 2016-01-20 1:37 AM, Nick Coghlan wrote:
> On 20 January 2016 at 10:38, Chris Angelico<rosuav at gmail.com>  wrote:
>> >Big one for the bike-shedding: Is this "capture as local" (the same
>> >semantics as the default arg - if you rebind it, it changes for the
>> >current invocation only), or "capture as static" (the same semantics
>> >as a closure if you use the 'nonlocal' directive - if you rebind it,
>> >it stays changed), or "capture as constant" (what people are usually
>> >going to be doing anyway)?
> The "shared value" approach can already be achieved by binding a
> mutable object rather than an immutable one, and there's no runtime
> speed difference between looking up a local and looking up a constant,
> so I think it makes sense to just stick with "default argument
> semantics, but without altering the function signature"
>
> One possible name for such a directive would be "sharedlocal": it's in
> most respects a local variable, but the given definition time
> initialisation value is shared across all invocations to the function.
>
> With that spelling:
>
>      def f(*, len=len):
>           ...
>
> Would become:
>
>      def f():
>          sharedlocal len=len

FWIW I strongly believe that this feature (at least the
"len=len"-like optimizations) should be implemented as an
optimization in the interpreter.

We already have "nonlocal" and "global".  Having a third
modifier (such as sharedlocal, static, etc) will only
introduce confusion and make Python less comprehensible.

Yury


More information about the Python-ideas mailing list