In Python, why is list[] automatically global?

前端 未结 5 1383
你的背包
你的背包 2020-11-28 12:56

This is a weird behavior.

Try this :

rep_i=0
print \"rep_i is\" , rep_i
def test():
  global rep_i #without Global this gives error but list , dict ,         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 13:44

    You only need to use global if you are assigning to the global name. Without global, an assignment creates a new local.

    There's nothing special about how global applies to a list—global simply influences scope and name resolution.

提交回复
热议问题