python 15 closure function

1. function object

Essence: the function as a variable to use, specific use can be divided into four areas

  • 1, can be assigned, the function name assigned A to B, B directly () function can be cited that the
f=func
print(f,func)
f()
  • 2, the function may be passed as an argument another function
def foo(x): # x = func的内存地址
    # print(x)
    x()

foo(func) # foo(func的内存地址)
  • 3, the return value of the function may be a function of
def foo(x): # x=func的内存地址
    return x # return func的内存地址

res=foo(func) # foo(func的内存地址)
print(res) # res=func的内存地址
res()
  • 4, elements of the container can be used as a type of
l=[func,]
# print(l)
l[0]()

dic={'k1':func}
print(dic)
dic['k1']()

Demonstration function object:

def login():
    print('登录功能')


def transfer():
    print('转账功能')


def check_banlance():
    print('查询余额')


def withdraw():
    print('提现')


def register():
    print('注册')


func_dic = {
    '0': ['退出', None],
    '1': ['登录', login],
    '2': ['转账', transfer],
    '3': ['查询余额', check_banlance],
    '4': ['提现', withdraw],
    '5': ['注册', register]
}
# func_dic['1']()


while True:
    for k in func_dic:
        print(k, func_dic[k][0])

    choice = input('请输入命令编号:').strip()
    if not choice.isdigit():
        print('必须输入编号,傻叉')
        continue

    if choice == '0':
        break

    # choice='1'
    if choice in func_dic:
        func_dic[choice][1]()
    else:
        print('输入的指令不存在')

2. nested functions

  • 1, nested function calls: calling a function of the process and call other functions
def max2(x,y):
    if x > y:
        return x
    else:
        return y


def max4(a,b,c,d):
    # 第一步:比较a,b得到res1
    res1=max2(a,b)
    # 第二步:比较res1,c得到res2
    res2=max2(res1,c)
    # 第三步:比较res2,d得到res3
    res3=max2(res2,d)
    return res3

res=max4(1,2,3,4)
print(res)
  • 2, the nested function definition: to define other functions within a function
def f1():
    def f2():
        pass
  • Seeking a circular perimeter or area seek
def circle(radius,action=0):
    from math import pi

    def perimiter(radius):
        return 2*pi*radius

    # 求圆形的求面积:pi*(radius**2)
    def area(radius):
        return pi*(radius**2)

    if action == 0:
        return 2*pi*radius

    elif action == 1:
        return area(radius)

circle(33,action=0)

3. The closure function

  • 1. The premise of
    the closure function and scope namespace = + + function nested function object
    core point: the name of the relationship is to find the function definition stage prevail
  • 2. What is the closure function
    "close" function refers to the function is inline function
    references the outer layer contains the name of the function scope "package" function refers to the function (instead of the global scope)
    concept based on function object, you can the function returns the call to go anywhere, but the relationship is in the scope of the definition of complete function has been determined, regardless of the location of the function call.
    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)
    closure function: namespace and scope application of a nested function +
def f1():
    x = 33333333333333333333
    def f2():
        print(x)
    f2()


x=11111
def bar():
    x=444444
    f1()

def foo():
    x=2222
    bar()

foo()
  • Closure function: function objects
def f1():
    x = 33333333333333333333
    def f2():
        print('函数f2:',x)
    return f2

f=f1()
# print(f)

# x=4444
# f()
def foo():
    x=5555
    f()

foo()

4. Use Closure

  • The Senate passed a plan:
def get(url):
     response=requests.get(url)
     print(len(response.text))

 get('https://www.baidu.com')
 get('https://www.cnblogs.com/linhaifeng')
 get('https://zhuanlan.zhihu.com/p/109056932')
  • Parameter passing Option Two:
def outter(url):
    # url='https://www.baidu.com'
    def get():
        response=requests.get(url)
        print(len(response.text))
    return get

Guess you like

Origin www.cnblogs.com/Franciszw/p/12550084.html