PEP 3107 and stronger typing (note: probably a newbie question)

Steven D'Aprano steve at
Mon Jul 9 00:17:04 CEST 2007

On Sun, 08 Jul 2007 08:49:26 -0400, Steve Holden wrote:

> Paul Rubin wrote:
>> Steve Holden <steve at> writes:
>>>> Python even leaks the index variable of list comprehensions (I've
>>>> mostly stopped using them because of this), though that's a
>>>> recognized wart and is due to be fixed.
>>> Wow, you really take non-pollution of the namespace seriously. I agree
>>> it's a wart, but it's one I have happily worked around since day one.
>> Well, the obvious workaround is just say "list(<some genexp>)"
>> instead of [<the same genexp>] so that's what I do.
> I think I'll start calling you Captain Sensible. But you are right, it 
> does avoid that extra little potential for errors.

I'd be thinking it should be Captain Paranoid. Why is list comp leakage
a worse problem than, well, having local variables in the first place?

def foo(x):
    y = something_or_other(x+1)
    z = do_something_else(y) + another_thing(y)
    # Oh noes!!! y still exists!!1!11!! What to do????
    return ("foo", y)  # OMG I meant to say z!!!

If your function has got so many lines of code, and so many variables that
this becomes a source of errors, your function should be refactored into
smaller functions.

As far as I can see, the only difference is that the list comp variable
isn't explicitly created with a statement of the form "name = value". Why
is that a problem?


More information about the Python-list mailing list