1. Namespace and scope in Python
In Python, a namespace (Namespace) is a dictionary used to store the mapping relationship between variable names and object references, and a scope (Scope) refers to the range of areas in a program where variables can be accessed.
There are 3 kinds of namespaces in Python: built-in namespace, global namespace and local namespace.
-
Built-in namespace: Contains objects such as built-in functions, modules, classes, and exceptions of the Python interpreter. These objects can be used directly without importing through import statements.
-
Global namespace: Contains all variables, functions, classes and exceptions defined in the current module. Functions, variables, and classes defined at the module level belong to the global namespace.
-
Local namespace: Contains all variable and parameter names inside the function. Whenever a function is called, a new local namespace is created for storing function parameters and local variables.
There are two kinds of scopes in Python: global scope and local scope.
-
Global scope: defined by the global namespace and can be accessed by any part of the program.
-
Local scope: Defined by the local namespace, variables defined inside a function can only be accessed within that function.
There are several ways to access variables in different namespaces in Python:
-
Variables defined in the global namespace can be used directly in any function or class within the module.
-
Variables defined inside a function can only be accessed inside that function and cannot be used outside the function.
-
When accessing a global variable inside a function, you need to use the global keyword to declare the variable as a global variable.
-
When accessing a variable defined in a nested function inside a function, you need to use the nonlocal keyword to declare the variable as a non-local variable.
2. Python code example
Below is a Python code example that demonstrates the basic concepts of namespaces and scopes in Python:
# 全局命名空间
x = 10
def func():
# 局部命名空间
y = 20
print("The value of x inside function:", x)
print("The value of y inside function:", y)
func()
print("The value of x outside function:", x)
# 访问全局变量
def func1():
global x
x += 5
print("The value of x inside function1:", x)
func1()
# 访问嵌套作用域变量
def outer_func():
x = 10
def inner_func():
nonlocal x
x += 1
print("The value of x inside inner function:", x)
inner_func()
print("The value of x inside outer function:", x)
outer_func()
In the above code, we define a global variable x
and a function func
, and local variables are defined inside the function y
. func
Global variables can be accessed when the function is called outside the function x
, and keyword declaration is required to access the global variable inside the function global
.
Next, we define a function func1
that internally uses global
keywords to declare x
the modification of the global variable, thereby realizing access to the global variable and modifying its value.
Finally, we define a nested function outer_func
, define variables inside , use keywords to declare variables as non-local variables x
in nested inner functions , and modify them. It can be seen from the output that the internal function modifies the variables defined in the external function .inner_func
nonlocal
x
x
Referring to the code example, we can draw the following conclusions:
-
The namespace in Python is a dictionary used to store the mapping relationship between variable names and object references. There are three namespaces in Python: built-in namespace, global namespace, and local namespace.
-
There are two kinds of scopes in Python: global scope and local scope.
-
In Python, global variables can be accessed anywhere within a module, while local variables are only accessed inside functions.
-
Inside the function, you can use the global keyword to declare the variable as a global variable, so that you can modify the value of the global variable inside the function.
-
When functions are nested, internal functions can use the nonlocal keyword to declare variables as non-local variables and modify them.