[Python-ideas] If branch merging

Chris Angelico rosuav at gmail.com
Thu Jun 11 12:56:35 CEST 2015


On Thu, Jun 11, 2015 at 1:06 AM, Chris Angelico <rosuav at gmail.com> wrote:
> Next plan: Change compiler_comprehension_generator() to use subscopes
> rather than a full nested function, and then do performance testing.
> Currently, this can only have slowed things down. Removing the
> function call overhead from list comps could give that speed back.
>

Or maybe the next plan is to hack in a "while cond as name:" handler.
It works! And the name is bound only within the scope of the while
block and any else block (so when you get a falsey result, you can see
precisely _what_ falsey result it was).

The surprising part, in my opinion, is that this actually appears to
work outside a function. The demangling doesn't, but the original
mangling does.

It doesn't play ideally with locals() or globals(); the former appears
to take the first one that it sees, and ignore the others (though I
wouldn't promise that; certainly it takes exactly one local of any
given name. With globals(), you get the mangled name:

while input("Spam? ") as spam:
    print(globals())
    break

Spam? yes
{... 'spam.0x7f2080260228': 'yes'...}

With brand new syntax like "while cond as name:", it won't break
anything to use a mangled name, but this is a backward-incompatible
change as regards exception handling and globals(). Still, it's a fun
hack.

Aside from being a fun exercise for me, building a Volkswagen
Helicopter, is this at all useful to anybody?

ChrisA


More information about the Python-ideas mailing list