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

Then you're wrong. The provided code is equivalent to the OP's, and the result is consistent.

Replies are listed 'Best First'.
Re^8: Closures and scope of lexicals
by LanX (Saint) on Oct 31, 2024 at 14:42 UTC
    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

      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