Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There's no block scope in Python. The smallest scope is function. Comprehension variables don't leak out, though, which causes some weird situations:

    >>> s = "abc"
    >>> [x:=y for y in s]
    ['a', 'b', 'c']
    
    >>> x
    'c'
    
    >>> y
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'y' is not defined
Comprehensions have their own local scope for their local variables, but the walrus operator reaches up to the innermost "assignable" scope.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: