[Python-ideas] Explicit variable capture list

Steven D'Aprano steve at pearwood.info
Tue Jan 19 19:14:25 EST 2016


On Wed, Jan 20, 2016 at 10:29:48AM +1100, Chris Angelico wrote:
> On Wed, Jan 20, 2016 at 3:47 AM, Guido van Rossum <guido at python.org> wrote:
> > I think it's reasonable to divert this discussion to "value capture". Not
> > sure if that's the usual terminology, but the idea should be that a
> > reference to the value is captured, rather than (as Python normally does
> > with closures) a reference to the variable (implemented as something called
> > a "cell").
> 
> +1. This would permit deprecation of the "def blah(...., len=len):"
> optimization - all you need to do is set a value capture on the name
> "len".

Some might argue that the default argument trick is already the One 
Obvious Way to capture a value in a function.

I don't think deprecation is the right word here, you can't deprecate 
"len=len" style code because it's just a special case of the more 
general name=expr function default argument syntax. I suppose a linter 
might complain if the expression on the right hand side is precisely the 
same as the name on the left, but _len=len would trivially work around 
that.



-- 
Steve


More information about the Python-ideas mailing list