Advanced Function - closure function

  • Object function: the function may be defined within the function returns to the global use, thus breaking the level limiting function.
  • Namespace and scope: Scope definition phase relationship when the function has been fixed dead, regardless of the calling location, find the scope of the relationship that is in any position when calling the function needs to go-defined functions.
def f1():
    x = 1

    def inner():
        print(x)
    return inner

func = f1()
x = 2

def f2():
    x = 3
    func()

f2()

1
What is Dian a closure
closure: the closure is closed (function of the internal function), it is a package comprising (a reference to the inner function rather than a variable outer scope of global scope). Closure means: a function of an internal function of the external reference, and not scope of global scope.
Tip: Before we are through external parameter value passed to the function, Closures provide another idea, wrap it up myself, Yo wrap, wrap wow.

def outter():
    x = 1
    def inner():
        print(x)
    return inner

f = outter()

def f2():
    x = 2
    f()

f2()

1.1 two kinds of ways as a function parameter passing

为函数传参的方式一:使用参数的形式

def func(x):
    print(x)


func(1)
func(1)
func(1)

1 1 1

Parameter passing as a function of two ways: packet function

def outter(x):
    x = 1

    def inner():
        print(x)
    return inner


f = outter(1)
f()
f()
f()
# 查看闭包的元素
print(F"f.__closure__[0].cell_contents: {f.__closure__[0].cell_contents}")

1 1 1 f.__closure__[0].cell_contents: 1


Application of two Dian closure function

Significance closure package: the function returns an object, not just a function object, outside the scope function also wrapped in a layer, which makes the function calls regardless of where, preferentially the outer layer wrapped their scope.

Application: delay calculation (turns out that we are mass participation, and now we are wrapped up), reptiles fields.
Without cumbersome codes closure:

import requests


def get(url):
    response = requests.get(url)
    print(f"done: {url}")
    
get('https://www.baidu.com')
get('https://www.baidu.com')
get('https://www.baidu.com')


get('https://www.cnblogs.com/linhaifeng')
get('https://www.cnblogs.com/linhaifeng')
get('https://www.cnblogs.com/linhaifeng')

done: https://www.baidu.com done: https://www.baidu.com done: https://www.baidu.com done: https://www.cnblogs.com/linhaifeng done: https://www.cnblogs.com/linhaifeng done: https://www.cnblogs.com/linhaifeng
The above approach is extremely complex, if we use the default parameters can only solve a URL, so we can consider the use of closures way.

import requests


def outter(url):
    def get():
        response = requests.get(url)
        print(f"done: {url}")
    return get

baidu=outter('https://www.baidu.com')
python = outter('https://www.python.org')

baidu()
baidu()

python()
python()

done: https://www.baidu.com done: https://www.baidu.com done: https://www.python.org done: https://www.python.org

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374771.html