Considerations for the issue
Nonlocal's Problem
Nonlocal variable range problem, you can get the variables of the upper function, if the upper function does not have this variable, then will go to the upper layer, but not to the global search.
Examples are as follows:
def Fun (): = 1 def fun2 (): = 2 def fun3 (): nonlocal a print (a) fun3 () fun2 () fun ()"" " Run result does not comment a=2 when the word is 2, after the comment is 1" ""
Questions about global scope and local scope
Example:
def Fun (): = 1 def fun2 (): + = 1 print(a) fun2 () fun () "" " This will cause an error, because if the Python interpreter is a local variable or local variable, it needs to be assigned the value " ".
About DIC increased J-time Dic.setdefault ("Key", "value")
This dic.setdefault ("key", "value") has the function of adding a key value pair, and it is important to note that this will have a return. The value returned is the value.
Because of the value of SetDefault, this increase is invalid when the key is present in the dictionary, but this returns the value of the key in the dictionary. If no key is present, the value of the SetDefault setting is returned.
Examples are as follows:
DiC = {}dic.setdefault ("key","value") Print (DIC) """ this is to increase the dictionary's key value """
About **kwargs This value, cannot pass in the value of **{3:4}, because the argument is equivalent to the variable, the variable cannot accept the pure number when the variable.
Example:
def func (* *Kwargs) :print(Kwargs )def(**{"A ": 2,"b": 3},**{3:4})
About the function default assignment of deep pits
def func (a,lst = []):
Lst.append (a)
return LST
The following questions:
Lst1 = func ("10")
Lst2 = func ("20", [])
Lst3 = func ("30")
Last Print (LST1,LST2,LST3)
Here is the result of the run
[' 10 ', ' 30 '] [' 20 '] [' 10 ', ' 30 ']
The reason is that when a parameter is passed into a variable value, the Python interpreter assigns a pair of images that are invariant, and when the function is repeatedly called, the same address is pointed. Therefore, the function default parameter should be passed as much as possible to the hash, that is, the immutable type.
There's a supplement.
The main contents of this section: 1. Function name of the operation, Class 2 object. Closures 3. The first knowledge of decorators
1. The function name of the operation, the first class object
The function name is a variable, but it is a special variable plus () is a variable that can be executed
The function name has the following characteristics:
1) The function name is a memory address,
Example:
def Fun (): Print (+) Print (fun)
The above code is the memory address of the print function.
2) function name can be assigned to other variables
Example:
The example is to assign a function name to B and then B to execute the function
def Fun (): Print (+= Funb ()
3) function names can be used as elements of a container class
Function names can be used as non-vegetarian for other container classes, container classes such as, lists, dictionaries, etc.
Example:
deffun1 ():Print("1")deffun2 ():Print("2")deffun3 ():Print("3") LST=[FUN1,FUN2,FUN3] forEinchLST:Print(e)"""variable name is only memory address, loop call, just show memory address"""
4. Function names can be used as arguments to functions
Example:
def Fun (): Print (" hehe ") def fun1 (FN): fn () fun1 (fun)
5. Function names can be used as return values for functions
Example:
def Fun (): def inner (): Print ("123") return = Fun () F ( ) "" " function return " ""
?. Closed Package
Python Learning Chapter Tenth