in reply to Re^8: Closures and scope of lexicals
in thread Closures and scope of lexicals

Saying Python's behavior is consistent to Perl's is at best an over simplification and can't be left uncommented.

Nope, no simplification. Capturing works exactly the same: Variables in scope remain accessible beyond their normal scope.

Replies are listed 'Best First'.
Re^10: Closures and scope of lexicals
by LanX (Saint) on Oct 31, 2024 at 15:07 UTC
    Your code was also over complicating Python, you never needed the variable i, because j never goes out of scope.

    subrefs = [] for j in range(1,4): def subref(): print( j ) subrefs.append( subref ) for subref in subrefs: subref()

    3 3 3

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      The OP's entire question is about the value of i. You can't just remove it! Chill out, man. You made a mistake, fine, but now you're compounding it time after time.