Python is a static scope language, although it is itself a dynamic language. That is, the scope of a variable in Python is determined by its location in the source code, which is similar to C, but the scope difference between Python and C is quite obvious.
Next, we'll talk about Python's scoping rules, which will also illustrate the difference between Python and C in terms of scope.
In Python 2.0 and earlier versions, Python supported only 3 scopes, the local scope, global scope, built-in scope, and in Python 2.2 python formally introduced a new scope---nesting scope; in Python 2.1, Nested scopes can be turned on as an option; the introduction of nested scopes is essentially Python's support for closures, and there are a lot of explanations on the web about closures, which are not covered in detail here. Accordingly, the variable lookup order is changed from previous LGB to LEGB (l:local,e:enclosing,g:global,b:built-in).
In Python, not every block of code can introduce a new scope, which is a lot different from C:
Copy Code code as follows:
#include <stdio.h>
int main () {
if (2 > 0) {
int i = 0;
}
printf ("I =%d", i);
return 0;
}
In this code, the IF clause introduces a local scope where the variable i exists in this local scope but is not externally visible, so the next reference to the variable i in the printf function throws a compilation error.
But that's not true in Python:
Copy Code code as follows:
In this code, if clause does not introduce a local scope, the variable i is still in the global scope, so the variable i is visible to the next print statement.
In fact, in Python, only modules, classes, and functions introduce new scopes, and other blocks of code do not introduce new scopes.
In Python, you don't have to declare it beforehand before you use a variable, but before you actually use it, it must already be bound to an object, and the name binding will introduce a new variable in the current scope while shielding the variable with the same name in the outer scope, regardless of where the name binding occurs in the current scope.
Copy Code code as follows:
The run result will show: Nameerror:global name ' I ' is not defined. Python first looks for the variable i in the local scope of function f, finds the failure, then finds the variable I in the global scope and the built-in scope, still fails, and eventually throws the Nameerror exception.
Copy Code code as follows:
i = 0
def f ():
i = 8
Print I
F ()
Print I
The results show: 8 and 0. i = 8 is a name binding operation, which introduces a new variable i in the local scope of function f, shielding the global variable I, so the print statement inside F sees the local variable i,f the external print statement to see the global variable I.
Copy Code code as follows:
i = 0
def f ():
Print I
i = 0
F ()
The results of the operation show: unboundlocalerror:local variable ' i ' referenced before assignment. In this example, the variable i in function f is a local variable, but when the print statement uses it, it is not bound to any object, so it throws an exception.
Copy Code code as follows:
Whether it is run interactively or as a script file, the results are shown: nameerror:name ' I ' is not defined. The output here differs from the previous example because it is in the top-level scope (the module scope). For the module code, the code is not preprocessed before it executes, but for the function body, the code has been preprocessed before it runs, so it can be sensed regardless of where the name binding takes place in the scope. Python is a static scope language, but the name lookup does occur dynamically, so it is not until the runtime that the name problem is discovered.
In Python, a name binding introduces a new variable in the owning scope and binds to an object. A name binding occurs under several conditions:
1. Parameter declaration: The parameter declaration introduces a new variable in the local scope of the function;
2. Assignment operation: The initial assignment of a variable will introduce a new variable in the current scope, and subsequent assignment will rebind the variable;
3. Class and function definitions: class and function definitions introduce the class name and function names as variables into the current scope, and the class body and function body form another scope;
4.import statement: Import statement in the current scope of the introduction of new variables, generally in the global scope;
5.for statement: The For statement introduces a new variable (loop variable) in the current scope;
6.except statement: The except statement introduces a new variable (exception object) in the current scope.
In Python, the scope introduced by a class definition is not visible to a member function, which is very different from C + + or Java, so in Python the member function wants to refer to the variable defined by the class body, which must be referenced by self or by the class name.
The addition of nested scopes can cause some code to compile but or get different results, where the Python interpreter will help you identify the areas where this might be causing problems and give a warning.
The locals function returns all local variables, but does not return the variables in the nested scope, and no function returns the variables in the nested scope.