[Python] foundation namespace

First, the definition namespace

  Variable name and value mapping

  Namespace is a dictionary implementation, key is the name of the variable, value is the value of the variable

Second, the classification of the namespace

  Python program execution process, while there will be 2-3 namespace activity (function call, there are three namespaces, after the end of the function call, there are two namespaces) 

  Variable depending on the location of the namespace into three categories:

    Local namespace: Local, refers to the function owned namespace, including the function parameters, local variables inside a function of the
    global namespace: Global, refers to a module (.py file) has a namespace, the function comprising , content class, global variables, imported module
    built namespace: Build-in, refers to the built-in functions and abnormal python.

    The same namespace, the variable name can not be repeated; different namespaces, independently of each variable name

Third, the life cycle of the namespace

  Local namespace: when creating function call, after the end of the function call to delete
  the global namespace: modules loaded when created, python interpreter quits when they were deleted
  built-in namespace: With the launch python interpreter created, python interpreter quits when they were deleted

Fourth, access namespaces

  about locals () function can be used to access a local namespace
  Globals () function can be used to access the global namespace

  Standard case is:
    local using about locals ()
    global using Globals ()

  Special case:
    the local, the use of globals (), to view all the content is not a global name space,
    but, in the local access to the contents of the global name space

Fifth, the order of creation namespace

  python interpreter start -> Create a built-in namespace -> Load Module -> Create global namespace -> function is called -> Create local namespace

Sixth, the order of destruction namespace

  Call the function ends -> destroy local namespace -> python interpreter quits -> destroy the global namespace -> Built-in namespace 

  To summarize: With the local namespace is dynamically created out of the called function, with the end of the function call dynamic destruction

VII Scope

  1, the concept of: for a variable which, during program execution, the variable can function (applicable) range
        * is a function, class, module generates a scope, the scope will not block

  2, the scope of the classification:
    the position variable, the scope can be divided into four categories (LEGB)
    the Local: a local scope (inside the scope of the function)
    Enclosing: enclosing scope (two nested functions, the outer insert scope set function)
    Free Join: global scope (scope module)
    Bulid-in: the built-in scope (built Python)

  3, find the scope of the rules, follow the rules LEGB

    According to the order in the local scope, enclosing scope, global scope, the role of the built-in lookup function, once found, they will not continue to find
    if none are found, an exception will occur in LEGB in: NameError: name 'abc' is not defined

    (1) access to the local scope local scope, the enclosing scope, global scope, the built-in variable scope
    (2) blocking access to the scope enclosing scope, global scope domain effect, the variable scopes built
        enclosing scope not have access to the variables in the local scope
    (3) global scope can have access to their own and built-scope variables
        do not have access to the contents of the enclosing scope and local scope 

Eight, the difference

  Local variables: the variables declared within a function, the function is only effective in the
  global variables: variables declared in the module, the entire module valid

  global variable is type immutable data, the function can not modify the global variable
  global variable is a variable when the data type, the function can modify the global variable
  (if the parameter is passed to a function of the global variable type data, a plurality of times during the function call, the global variables will change)

Nine, keyword

  global: local scope tag in a global variable is a variable
       may be a global variable into local variables

  nonlocal: a flag variable is a variable in the local scope of the outer closure scope
      may be modified outer layer (non-global) variables

Guess you like

Origin www.cnblogs.com/Tree0108/p/12110103.html