List comprehension rebinds names even after scope of comprehension. Is this right?

后端 未结 6 2165
清酒与你
清酒与你 2020-11-22 04:03

Comprehensions are having some unexpected interactions with scoping. Is this the expected behavior?

I\'ve got a method:

def leave_room(self, uid):
           


        
6条回答
  •  轮回少年
    2020-11-22 04:51

    In python3 while in list comprehension the variable is not getting change after it's scope over but when we use simple for-loop the variable is getting reassigned out of scope.

    i = 1 print(i) print([i in range(5)]) print(i) Value of i will remain 1 only.

    Now just use simply for loop the value of i will be reassigned.

提交回复
热议问题