Skip to content

Files

Latest commit

 

History

History
28 lines (21 loc) · 565 Bytes

cell-var-from-loop.md

File metadata and controls

28 lines (21 loc) · 565 Bytes

Pattern: Defining closure variable in loop

Issue: -

Description

A variable used in a closure is defined in a loop. This will result in all closures using the same value for the closed-over variable.

Example of incorrect code:

def bad_case():
    """Closing over a loop variable."""
    lst = []
    for i in range(10):
        print(i)
        lst.append(lambda: i)  # [cell-var-from-loop]

Example of correct code:

def good_case():
    """No problems here."""
    lst = []
    for i in range(10):
        lst.append(i)