Scope-related function 1--

Scope-related function 1--

Why use functions?

Reduce code redundancy and increase code readability

1. The functions of some of the concepts

  • def is an executable code. def is an executable statement - function does not exist (after the function is called) until the function is only present after running def. def statement written in the module file, generated when the functions defined in the module file is first introduced.
  • def create an object and assign it to a variable name (ie the name of the function). When run when the def statement, it will generate a new function object and assigns it to the function name, like all of the assignment, the function name into a reference to a function.
  • return a result to send the object to the caller. When the function is called, it calls this function to stop running until the completion of his work, it will function returns control to the caller. Function is calculated by the value of the return statement passed to the caller.
  • yield returns a result object to the caller, and remember the state it left off
  • global declare a module-level variables.
  • nonloca L declared a closed function variables. Some names have been nonlocal statement allows a function to assign a grammar closed def statement of scope.

2. Scope

python create, alter, or lookups or variable names are in a namespace (a variable name to save the place) in. In other words, the variable name in the code position is assigned determines the variable name can have access to the range. (In the code to place a variable assignment determines this variable exists with that name space).

By default, all variable names are a function of space associated with a named function:

1. The name of a variable defined within the code def can be used in the def. You can not refer to such a variable name outside the function.

def add(value1, value2):
    result = value1 + value2
add(2, 4)
print(result)
报错:
NameError: name 'result' is not defined

Variable names and variable names in 2.def outside def do not conflict, they are completely different variables.

result = 0
def add(value1, value2):
    result = value1 + value2
    print(result)
add(2, 4)
print(result)
运行结果:
6
0

3. A variable can be assigned at three different places, corresponding to three different scopes:

  • A variable within def assignment, it is positioned within this function

  • A variable in a nested def assignment for the nested function, it is non-local

  • In addition def assignment, it is the entire global file

    x = 0                 # 全局变量
    def func1():
      x = 1           # 对于func2来说,x = 1是非本地的 
      def func2():
          x = 2       #本地的
2.1 Scope law
  1. The module is embedded global scope (global variable external module becomes a target attribute (the subsequent reflection) can be used like a variable in a module

  2. Scope of the global scope is limited to a single file

  3. Every call to a function creates a new local scope

  4. Variable name assignment unless a declaration or local variables as global variables, or variables are local

    x = 0
    def func():
        global x
        x = 1
    func()  
    print(x)
    #结果:
    1
    #func函数被执行了,并声明了一个全局变量x,x=1,作用与全局,覆盖了之前的x=0
    #如果func函数没有被执行,结果为0
3. Variable name resolution: LEGB principle (or LNGB)

When using the variable name in the function, python sequentially search scopes 4 - local scope (L), a layer of a local scope def or lambda (E) (for the inner layer is non-local, N, nocal ), global scope (G), and finally built scopes (B)

When given a variable in the function name assignment when (rather than a reference to them in an expression), Python always create or change the name of a local variable scope, unless it has been declared as a global variable in the function.

4.global and nonlocal

Global variables in the name of the function without internal statement also can be referenced (part LEGB rules)

If the global variable is assigned within a function, it must be declared. (When you want to create (or modify) a global variable within a function)

nolocal x x can only act on the scope of local variables of the outer layer of the nearest nested function, if not will find an error

a = 1
def func():
    a = a + 1
func()
#报错:
UnboundLocalError: local variable 'a' referenced before assignment
#局部变量a在引用前没有定义

nonlocal understanding of local and non-local, nonlocal very good use

a = 0
def func1():
    a = 1     
    def func2():
        print(a)
    func2()
func1()
#a=1对于func1是本地,a =1对于func2是非本地本地,但a=0,对于func1不是非本地。
5.闭包(closure)

Closure called factory function: a function value of the variable can remember nested scopes

def f1();
    x = 1
    def f2():
        print(x)
    return f2
a = f1()
a()
#命名为函数f2的函数的调用是在f1运行之后,f2记住了f1中嵌套作用域中的x,尽管f1已经不处于激活状态。

def f1(m):
    def f2(n):
        print(m*n) 
    return f2
t1 = f1(2)
t2 = f1(3)
t1(2)   # 4
t2(3)   #9
#内嵌的函数f2记住了f1函数内部地变量n的值,尽管在调用执行t1(还有t2)时f1已经返回了值并推出,

def fun(x):
    inner = (lambda y:x*y)
    return inner
t = fun(2)
print(t(5))  #10

Guess you like

Origin www.cnblogs.com/notfind/p/11460605.html