in reply to Re^7: 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.

JS on the other side is very consistent to Perl, especially when using let

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

Replies are listed 'Best First'.
Re^9: Closures and scope of lexicals
by ikegami (Patriarch) on Oct 31, 2024 at 14:50 UTC

    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.

      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.