the younger brother scope of the namespace
Here's a point of view where everything is object in Python, and the variable points to the object.
A variable can be a class name, a function name, a variable that stores data ...
objects can be classes, encapsulated snippets of code (functions), data ...
Name space
a namespace is a mapping from a name to an object. most namespaces in Python are currently implemented in the form of a dictionary. Variable names are "Keys", objects ( built-in functions, built-in constants, built-in types, functions, classes, variables ) are "values".
Each namespace is independent and has no relationship, so a namespace cannot have duplicate names, but different namespaces can have duplicate names without any effect.
namespaces are created time and lifetime. for Python built-in namespace(built-in namespaces-built-in functions, built-in constants, built-in types), it is created when the Python interpreter is started, Deleted when the interpreter exits;
For the global namespaceof a Python module, it is created when the module is referenced and exits when the interpreter exits;
For a function's local namespace (local scope), it is created every time the function is called and is deleted when the function returns.
Scope
The scope is just a property of the namespace representation size (applicable range).
A scope is a paragraph or segment of a Python program (text) where a name in a namespace can be directly referenced. This scope is the scope of this namespace.
The scope in Python is divided into 4 scenarios:
- l:local, local scope, which is the variable defined in the function;
- E:enclosing, the local scope of the nested parent function, which is the local scope of the ancestor function that contains the function, but not the global;
- G:globa, a global variable, is a variable defined at the module level;
- B:built-in, the variables inside the system fixed module, such as int, ByteArray, etc.
order of precedence for search variables
local scope > Outer scope > Global >python Built-in scope in the current module, that is, l>e>g>b.
B = STR ("b:built-in built-in scopes") G="G:globa, a global variable, is a variable defined at the module level"PrintB#Global >python built-in scopes in the current module look for b Print(G)#Global Find G in the current modulePrintE#I can't find the EPrintL#I can't find the Ldeff (): E="e:enclosing, the local scope of the nested parent function" PrintB#outer scope > Global >python Built-in scope in current module find B Print(G)#outer scope > Global find G in current module PrintE#outer scope Find e PrintL#I can't find the L defZ (): L="l:local, local scope, which is the variable defined in the function" PrintB#Scope Local > Outer scope > Global >python Built-in scope in the current module find B Print(G)#Scope Local > Outer scope > Global find G in current module PrintE#Scope Local > Outer scope find e PrintL#Scope Local Find L
Note: in Python, only modules (module), Classes (Class), and functions (Def, Lambda) introduce new scopes, and other blocks of code (such as if, try, for, and so on) do not introduce new scopes.
Modification of variables
In the scope local > Outer scope > Global >python Built-in scope in the current module , the scope of the outer scope cannot be modified directly by the inner scopes variable , to modify the global keyword and the nonlocal keyword to be used
Global keyword
When the modified variable is on the global scope, use global to declare it first.
nonlocal keywords
The variables declared by the Global keyword must be on the global scope, not nested scopes, and when you want to modify the nested scope (enclosing scope, outer non-global scope) You need to nonlocal the keyword.
B = STR ("b:built-in built-in scopes") G="G:globa, a global variable, is a variable defined at the module level"deff (): E="e:enclosing, the local scope of the nested parent function" defZ (): L="l:local, local scope, which is the variable defined in the function" Globalb b="Modifying variables of global scope"nonlocal e e="Modifying a variable of a nested scope"
Welcome comments, tomatoes, eggs are smashed over it!!!
I and Python's py Deal "" "" the Little Brother scope of the namespace