07-04 and the closure function object

[TOC]

A function object

Refers to the function object can be treated as a function of 'data' to handle, particularly use can be divided into four areas, as we

Illustration: spoof 26
07-04 and the closure function object

1.1 function can be referenced

>>> def add(x,y):
...     return x+y
... 
>>> func=add
>>> func(1,2)
3

1.2 as a function of container type element

>>> dic={'add':add,'max':max}
>>> dic
{'add': <function add at 0x100661e18>, 'max': <built-in function max>}
>>> dic['add'](1,2)
3

1.3 can function as a parameter another function

>>> def foo(x,y,func):
...     return func(x,y)
...
>>> foo(1,2,add)
3

1.4 function return value may be a function of

>>> def bar():
...     return add
...
>>> func=bar()
>>> func(1,2)
3

Illustration: spoof 27
07-04 and the closure function object

Two closure function

2.1 and closure package

Based on the concept of the object function, the function can be returned to any location to call, but the relationship is in the scope of the definition of complete function has been determined, regardless of the location of the function call.

x=1

def f1():
    def f2():
        print(x)

    return f2

def f3():
    x=3
    f2=f1() #调用f1()返回函数f2
    f2() #需要按照函数定义时的作用关系去执行,与调用位置无关

f3() #结果为1

That is the function are treated as data processing, always comes with the scope prevail. If the function contains embedded reference to the external scope (not global scope) variables, then the 'nested function' is a function closure referred closures (Closures)

x=1
def outer():
    x=2
    def inner():
        print(x)
    return inner

func=outer()
func() # 结果为2

Illustration: spoof 28
07-04 and the closure function object

Can function closure properties, external variables to view the package closure function

>>> func.__closure__
(<cell at 0x10212af78: int object at 0x10028cca0>,)
>>> func.__closure__[0].cell_contents
2

"Closed" is a function representing the internal outer "package" represents the function 'wrapped' with reference to the outer layer scope. Thus wherever closure function call, variable use is still wrapped in its outer layer.

Illustration: spoof 29
07-04 and the closure function object

2.2 The use of closures

So far, we have two kinds of body mass as a function of the value of ways, one is a direct pass values ​​as parameters, another is a function of the value of contracted out

import requests

#方式一:
def get(url):
    return requests.get(url).text

#方式二:
def page(url):
    def get():
        return requests.get(url).text
    return get

Tip: requests module is used to simulate the browser sends a request to the Web site and page content downloaded to the local, need to install: pip3 install requests

Illustration: spoof 30
07-04 and the closure function object

Comparison of two ways, a way to download the same page in the need to repeat the incoming url, and ways need to pass two values ​​once, you'll get closure function that contains the specified url, and after calling the closure function without having to re-transmission url

  # 方式一下载同一页面
get('https://www.python.org')
get('https://www.python.org')
get('https://www.python.org')
……

# 方式二下载同一页面
python=page('https://www.python.org')
python()
python()
python()
……

This characteristic closure function sometimes referred to as lazy evaluation. The value of the function using packet mode, in the following decorator also of great use

Illustration: spoof 31
07-04 and the closure function object

Guess you like

Origin blog.51cto.com/egon09/2461546