Python --- 2 --- Wang Wei function

#### Scope
`` Python `
# 1 Scope: regional variables to take effect
. # 2 scope of classification
. a global scope (global variable)
- to create a global scope when the program is executed, destroyed at the end of program execution
- all functions other than regions are global scope
- in global scope defined variables, are part of the global variables, global variables may be accessed at any point in the program
-globals () returns the current global variables
- the module level, about locals () and globals () is the same dictionary
b function scope (local variables).
- function scope is created when the function is called, destroyed at the end of call
- every function is called once will produce a new function scope
- defined function variables in scope, are local variable, it can only be accessed within the function
# 3. Look variable
a. First, look for variables in the current scope, if there is used
b. If you do not continue to go on a scope to find, if you use
c. If you still do not continue to go on a level scope to find, and so
d. until you find the global scope, it is still not found, an exception is thrown
# 4. Internal functions use global variables
when referenced *** global variables, no global statement, or when using this global variable modified, requires global statement
# 5.global: survival indicates that a particular variable in the global scope and should be re-bound in which
-global: After modifying variables, identifies the variable is a global variable, the variable is modified modify global variables
# 6.nonlocal: specific variables indicate survival in the enclosing scope and should be re-bound therein
-nonlocal: the modified variable identifies the variable is a function of the local variables, if the function does not exist in a the local variable, nonlocal position error occurs (the topmost function uses nonlocal variables must be modified error)
# 7.global and nonlocale difference
1.global can be used anywhere, including uppermost layer functions and nested functions, even if it is not defined before, after global modification can also be used directly, 2.nonlocal only be used for embedded function sets, and the outer layer function defined in the corresponding local variable, or an error occurs
`
#### Namespaces
`` Python `
# 1 namespace: a name to an object from the mapping between
a namespace is the nature of the dictionary.
b store variables.
# 2. Namespace Classification
a. A set of built-in functions (including print () built-in functions and the like built abnormal)
B. Global name module (also a set of attributes of the object namespace form)
C. Function calls the local name
# 3. Absolutely no relation between names in different namespaces
# 4 namespace created at different times have different lifetimes
a built-in namespace names is created when the Python interpreter starts.
B module's global namespace in the module definition is read in creation; Typically, module namespace will continue until the interpreter quits
c. a function of the local namespace is created when the function is called, and deleted when the function returns or throws an error is not a function of internal processing. Of course, each recursive call will have its own local namespace
# 5.locals get the current namespace
`` `
#### return function return value
Python `` `
. # 1 Returns: Returns the result after the function is executed, the return value of the function is specified by the return of
# 2.return followed what values, what the function returns a value, you can return any object with
# 3 If only. He wrote a return or do not write return, None is equivalent return
# 4. in the function, the code will not be executed after the return, return once the execution of the function ends automatically
def function(*args):
    result=0
    for i in args:
        result += i
    return result
function(10,20,30)
The output #
60
def function1():
    print("python")
    def function2():
        print("hello world")
    return function2
print(function())
# Output
Python
<function1 function. <About locals> .function2 AT 0x000001F787477318>
`` `
#### recursive function
`` Python `
# 1 recursive function: in the function call his own
# 2 recursive function of two elements.
#A baseline condition.
Minimizing the problem can be decomposed into when baseline conditions are satisfied, recursion is not in execution the
#b. recursive condition
will continue to break down the problem conditions
# 10 3 factorial
DEF function (A):
    IF A ==. 1:
        return. 1
    return function n-* (. 1-n-)
 Print (function (10))  
The output #
3628800
`` `
 

Guess you like

Origin www.cnblogs.com/niaocaizhou/p/12054485.html