2020 Python 연습 12 - 함수 대상과 패키지 함수 (1)

7993 단어
@2020.3.20 
1、         if     
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+=1
                    return x


                
            print(couter()) # 1
            print(couter()) # 2
            print(couter()) # 3
            print(couter()) # 4
            print(couter()) # 5
def f1():
    x=0
    def counter():
        nonlocal x
        x+=1
        return x
    return counter

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

좋은 웹페이지 즐겨찾기