Python: Difference between 'global' & globals().update(var)
问题 What is the difference between initializing a variable as global var or calling globals().update(var) . Thanks 回答1: When you say global var you are telling Python that var is the same var that was defined in a global context. You would use it in the following way: var=0 def f(): global var var=1 f() print(var) # 1 <---- the var outside the "def f" block is affected by calling f() Without the global statement, the var inside the "def f" block would be a local variable, and setting its value