[Python-ideas] If branch merging

Chris Angelico rosuav at gmail.com
Thu Jun 11 21:19:29 CEST 2015


On Fri, Jun 12, 2015 at 5:10 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 06/11/2015 03:56 AM, Chris Angelico wrote:
>
>> while input("Spam? ") as spam:
>>      print(globals())
>>      break
>>
>> Spam? yes
>> {... 'spam.0x7f2080260228': 'yes'...}
>
>
> Having names not leak from listcomps and genexps is a good thing.
>
> Having names not leak from try/execpt blocks is a necessary thing.
>
> Having names not leak from if/else or while is confusing and irritating:
> there is no scope there, and at least 'while' should be similar to 'for'
> which also does a name binding and does /not/ unset it at the end.
>
>
>> Aside from being a fun exercise for me, building a Volkswagen
>> Helicopter, is this at all useful to anybody?
>
>
> I would find the 'as NAME' portion very useful as long as it wasn't
> shadowing nor unset.
>

Sure. Removing the scoping from the "while cond as target" rule is
simple. Just delete a couple of lines of code (one at the top, one at
the bottom), and it'll do a simple name binding.

On the subject of try/except unbinding, though, there's a surprising
thing in the code: the last action in an except clause is to assign
None to the name, and *then* del it:

try: suite
except Something as e:
    try:
        except_block
    finally:
        e = None
        del e

Why set it to None just before delling it? It's clearly no accident,
so it must have a reason for existing. (With CPython sources, it's
always safest to assume intelligent design.)

ChrisA


More information about the Python-ideas mailing list