I have told you some knowledge about functions before, but I forgot to mention a very important point, that is, the scope of variables. This knowledge is not only applicable to functions, but also to all Python programs in the trial scope.
Before formally writing a program, you must be clear about this, otherwise it is easy to make mistakes
First clarify a concept, what is a variable
Variables can be seen as a name pointing to a value, just like the dictionary we talked about before, but you can't see this dictionary. Of course, this is a very popular explanation, but it is not far from the truth.
There is a built-in function for vars in Python that returns this invisible "dictionary"
For example the following code
In [1]: a = 100 In [2]: destroy = vars () In [3]: distrue["a"] Out[3]: 100 In [4]: distrue["a"] += 100 In [5]: a Out[5]: 200
Isn't it very magical to turn the variable into a dictionary corresponding to the key value, and you can also change the value through the index
It should be noted here that according to the official documentation, the dictionary returned by vars should not be modified, which will make the result uncertain
We call this invisible dictionary namespace or scope
scope
We now give the following code
In [7]: a = 100 In [8]: def func_one(): ...: a = 200 ...: In [9]: func_one() In [10]: a Out[10]: 100
At the beginning, we define a variable and assign 100 to a, and then we define a method later that assigns 200 to a
After that, we called this method, but the value of a did not change at the end. This involves the problem of variable scope. We will explain it slowly.
First of all, when we call func_one, a new namespace is created for the code block in func_one to use, and the assignment statement a = 200 is executed in the scope inside this function, that is, a local namespace. variables that affect the outer scope (or global),
Variables used inside a function are called local variables, and those outside the function are called global variables
Parameters are similar to local variables, so it doesn't matter if they are named the same as global variables
For example, the following output function
In [12]: def output(x): ...: print(x) ...: In [13]: x = 10 In [14]: y = 20 In [15]: output(y) 20 In [16]: output(x) 10
function access global variable
Just read the words, very simple
For example the following code
In [18]: def linkval(words): ...: print(words + external) ...: In [19]: external = " susmote" In [20]: linkval("hello") hello susmote
However, in this reminder, this method is still used with caution, it is easy to cause bugs
At this time, we have encountered a new problem. If the local variable and the global variable are the same, there will be a problem of covering.
Change the above code a little and the problem will appear
In [21]: def linkval(words): ...: external = " jack ma" ...: print(words + external) ...: In [22]: external = " susmote" In [23]: linkval("hello") hello jack ma
In other words, global variables are obscured by local variables
If you really need it, you can use the globals function to access global variables, which will return a dictionary containing global variables, which means you can get the value through the key index of the dictionary
(relative locals returns a value containing local variables)
Or the above code, we make a little change, the result will be very different
In [24]: def linkval(words): ...: external = " jack ma" ...: print(words + globals()['external']) ...: In [25]: external = " susmote" In [26]: linkval("hello") hello susmote
Define global variables in functions
Generally, we define variables in functions, which are local variables by default, but we can directly declare the defined variables as global variables. At this time, we must use the keyword global.
In [32]: a = 100 In [33]: def func_one(): ...: global to ...: a += 100 ...: In [34]: func_one() In [35]: a Out[35]: 200 In [36]: func_one() In [37]: a Out[37]: 300
nesting of scopes
There is a concept not mentioned before, that is, the definition of functions can be nested, similar to a function, and functions can also be defined, similar to the following code
In [45]: def func_one(): ...: def func_two(): ...: print("Inner function") ...: print("Outer function") ...: func_two() ...: In [46]: func_one() outer function inner function
Nested functions don't really do much, but one of his prominent features is still good, using one function to create another function
For example the following code
In [48]: def adder(factor): ...: def addByFactor(number): ...: return number + factor ...: return addByFactor ...: ...: In [49]: adder(10)(10) Out[49]: 20 In [50]: adder(100)(100) Out[50]: 200
A function is located in another function, and the outer function returns the inner function, which is not called
Among them, the most important point is that the returned function can access the scope where it is defined, that is to say, when he returns, he also brings his own environment (related local variables)
In practice, this local variable from the outer scope can be accessed in the inner function
We call this kind of function a closure
If you want to assign values to variables in the outer scope, you can use the nonlocal keyword, which is somewhat similar to global
That's all I have to say about scope, and it's easy to understand with a simple exercise