News for hackers. Stuff that matters.
Re:Understanding a Python Closure Oddity by b3orn on Monday June 17, 2024 @11:20AM (#40706498)
It's unrelated to the for loop, the for only changes the variable, there are other ways you can do that and you'll run into the same "oddity"

    def test():
        n = 0
        def a():
            return n
        n = 1
        def b():
            return n
        return a, b

    a, b = test()
    a() == 1
    b() == 1
Re:Understanding a Python Closure Oddity by hcmarker on Monday June 17, 2024 @11:48AM (#40706881)
I get why it behaves like this, but I find it odd that Python claims to have "lexical scoping". This is not lexical (as you read it). This is based on somewhat hidden reference lookups.
Re:Understanding a Python Closure Oddity by abecedarius on Monday June 17, 2024 @08:40PM (#40712870)
> for changes the variable

That's a design decision, not a law of nature. JS 'for let' lacks this problem; probably the committee would have named it just 'for' if not for legacy.