1. Global variables and local variables
These two variables are the same as variables in other languages. Global variables are simply variables that can be used throughout the code. Its scope is the entire function, and Local variables have a limited scope, often within a code area.
It should be noted that if you just call the value of the global variable in the function without changing its value, there is no problem at all, but if you want to change the value of the global variable in the function without special processing, The Python language handles this situation by automatically generating a local variable with the same name as the called global variable, which means that the global variable is shielded, and operations on the variable will not affect the value of the global variable. (Although it looks like it has changed)
For example, in the following program, although the value of count is changed in the function, the value of count printed outside the function is still 5.
count=5def Myfun(): count=10 print(count) Myfun() print(count)
If you have to modify the value of a global variable inside the function, you can use the keyword global to modify the variable inside the function. This means that the operation is an operation on the global variable, rather than generating an Global variables are the same as local variables.
print("======使用global之后的变量======") count=5def Myfun(): global count#声明与赋值不能一块进行 count=10 print(count) Myfun() print(count)
2. Embedded (internal) function
In short, an embedded function is a function defined inside the function
It is worth noting that the internal function can only be called outside of it Function call, but cannot be called outside. In other words, who has the right to use it within who owns it.
print("======内部函数的使用======")def fun1(): print("fun1()正在被调用") def fun2(): print("fun2()正在被调用") fun2() fun1()
3. Closure
A closure is the parameter used by an embedded function to call its external function.
You need to pay special attention when calling this function.
def funX(x): def funY(y): return x*y return funY print(funX(5)(8))
4. Variable problems in closures
The following code will report an error when executed. Because the parameters of the external function are called inside the embedded function, and the parameter x is a global variable for the function Fun2(), due to the shielding effect, the function error occurs.
def Fun1(): x=5 def Fun2(): x*=x return x return Fun2()#Fun1()
There are two ways to solve the above problem:
One is to use non-stack data structure to solve the problem
The second is to use the nonlocal keyword to solve the problem
#一种解决办法就是使用非栈存储,使用序列等来存储def Fun1(): x=[5] def Fun2(): x[0]*=x[0] return x[0] return Fun2() print(Fun1(),"\n")#在一种解决办法就是使用nonlocal关键字def Fun3(): x=5 def Fun4(): nonlocal x x*=x return x return Fun4() print(Fun3())
You need to think carefully about the variables in the function. After all, there are some differences from what you learned before.
The above is the content of the seven variables and built-in functions of Python's zero-based introduction. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!