variables - Python: Difference between 'global' & globals().update(var) -


global var or calling globals () What's the difference .update (var) .

thanks

when you say

  Global var  

You are telling Python that they are exactly what was defined in a global context. You will use it in the following way:

  var = 0 def f (): global var var = 1f () print (var) # 1 & lt; ---- Outside the outside, "Def F" is affected by calling Block F ()  

, there will be a local variable inside the "Def F" block, and set its value There will be no impact on the outside of the "Def F" block.

  var = 0 def f (): var = 1f () print (var) # 0 & lt; The taboo is unaffected outside the "FF" block.  

When you say globals.update (var) I'm guessing you actually (Global). Update (var) Let's separate it.

Globals () gives a triangle object The keys of the keys are the names of the objects, and the value of the dotted values ​​is the value of the connected object.

Each doc is a method called "update", then globals (). Update () is a call to this method. The update method is expected to have at least one argument, and this argument is likely to be like a dict if you call Python

  globals (). Then var is better, and you are telling Python to update the global  

For example:

Pre> #! / Usr / bin / env python # is the original global () dot print (globals) # {'__biltins__': & lt; Module '__builtin__' (implicit)>, '__name__': '__main__', '__file__': '/ home / unutbu /pybin/test.py', '__doc__': none} var = {'x': 'howdy '} Globals (). Updated (var) # var and 'x' print (globals) in global () dictionary # {'var': '___'___', '__file__': '' '' '' '', 'X': 'Howdy', '__biltions__': & lt; Module '__biltin' (implicit)>, '__name__': '/home/unutbu/pybin/test.py', '__doc__': none} # Take and see, you have not defined x x = Howdy! Print (x) Howdy


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -