News for hackers. Stuff that matters.
Re:Pattern matching accepted for Python by pansa2 on Tuesday February 09, 2021 @05:47PM (#26083521)
There are some exceptions to this - for example, the iteration variable(s) in a list comprehension are only in scope within the comprehension.
Re:Pattern matching accepted for Python by amirkdv on Tuesday February 09, 2021 @11:50PM (#26085839)
Worth noting that this was _fixed_ in Python 3.

In Python 2.7 you get this:

    >>> my_list = [x for x in 'hello']
    >>> x
    'o'
Re:Pattern matching accepted for Python by pdw on Tuesday February 09, 2021 @06:14PM (#26083803)
Thanks, I didn't realize that. That makes their choice really baffling.
Re:Pattern matching accepted for Python by _nosewings_ on Tuesday February 09, 2021 @07:34PM (#26084454)
The explanation is that (in Python 3) list comprehensions are really just syntactic sugar for generator expressions. In Python, "scope" is really a synonym for "dictionary attached to some object", so generators can have local variables (since they have their own internal scope), but purely syntactic constructs cannot.