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

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

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

    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.