Ways to avoid that for-loop variables cut into Python's global namespace
问题 I am wondering if there is a way to avoid that for-loop variables cut into Python's global namespace? The only solution I could come up with so far would be using closures, such as list comprehensions E.g., for the following code: i = 1 print([i for i in range(5)]) print(i, 'i in global') j = 1 for j in range(5): if j == 4: print(j, 'j in for-loop') print(j, 'j in global') prints [0, 1, 2, 3, 4] 1 i in global 4 j in for-loop 4 j in global EDIT: I assume that the for-loop namespace falls into