关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

Python:函数对象和闭包函数

发布时间:2020-03-21 00:00:00

 @2020.3.20 

1
def foo():print('foo')def bar():print('bar')

dic={'foo':foo,'bar':bar,
}while True:
    choice=input('>>: ').strip()if choice in dic:
        dic[choice]()

 

 2、编写计数器功能,要求调用一次在原有的基础上加一
        温馨提示:
            I:需要用到的知识点:闭包函数+nonlocal
            II:核心功能如下:def counter():
                    x+=1return x


        要求最终效果类似print(couter()) # 1print(couter()) # 2print(couter()) # 3print(couter()) # 4print(couter()) # 5
def f1():
    x=0def counter():
        nonlocal x
        x+=1return xreturn counter

counter=f1()print(counter())print(counter())print(counter())print(counter())print(counter())

 


/template/Home/Zkeys/PC/Static