[Python-ideas] Object grabbing

Eric Snow ericsnowcurrently at gmail.com
Mon May 2 20:06:13 EDT 2016


On Mon, May 2, 2016 at 8:35 AM, Bruce Leban <bruce at leban.us> wrote:
> Using a single letter (or short) variable name works well most of the time
> but has one problem: the variable can leak. It's easy to forget to write the
> del statement. Imagine a block statement that had the effect of deleting any
> variables initialized in the block. That is (using the with keyword in this
> example but there are other choices):
>
>     x = foo()
>     with:
>         y = x + 1
>         x = y + 2
>
> is equivalent to:
>
>     x = foo()
>     y = x + 1
>     x = y + 2
>     del y
>
> or after optimization:
>
>     x = (foo() + 1) + 2
>
> [Obviously more useful with real code but this is sufficiently illustrative.
> Also I imagine someone might say it's just foo() + 3 but I don't know the
> type of foo().]

Hmm.  That reminds me of the "given" syntax (PEP 3150:
https://www.python.org/dev/peps/pep-3150/).  This is like allowing
blocks of code to be treated as first-order, a la Ruby.  Then again,
functions give us just about all we need, and the "given" syntax
basically gives us multi-line lambdas. :)  Perhaps it's time to dust
off that proposal.

-eric


More information about the Python-ideas mailing list