20 lines of code in the Python clear global and local variables

  python global and local variables in the
  local variables are variables defined in the body of the function
  Global variables are defined externally
  example:
  a. 1 =
  DEF F (): B = 2
  wherein a is a global variable, b is a local variable. Local variables valid only inside the body of the function, the function body inaccessible external, global variables effective on the following code.
  Using global variables within the function body
  global variables can be used directly in the content portion of the body of the function, you can access them directly. However, it should be noted that if the implementation of the assignment immutable types of data in a function, it will not affect the external global variable, because it is equivalent to creating a new local variable and a global variable of the same name. For variable type, if the assignment statement, it will not have an impact outside, but if you use, it will have an external impact.
  For example the following code:
  G = B _. 3; g_l1 = [1,2]; g_l2 = [l, 2,3]
  DEF T1 ():. _ G _ Ll 2g = B = [] G _ L2 of the append (. 7 )
  T1 (g_b, g_l1, g_l2)
  print (g_b, g_l1, g_l2)
  global keywords
  mentioned above, if the assignment statement is equivalent to create a new variable inside a function, and point to it again. Sometimes, however, we hope that this variable references to external global variables. In the assignment, we give a new pointer to a global variable. At this point, we can use the global keyword to indicate internal functions are global variables that I use. Use as follows:
  G_B. 3 =
  DEF T1 (): = 2 G_B Global G_B
  T1 ()
  print (G_B)
  At this point, you'll find a global variable g_b also been redirected because the global gb instructions specified in the g_b function is an external function.

Guess you like

Origin www.cnblogs.com/phploser/p/12286781.html