#函數的嵌套調用
# def max(a,b): # return a if a>b else b # def the_max(x,y,z): # c = max(x,y) # return max(c,z) # print(the_max(1,2,3))
#函數的嵌套定義
#內部函數可使用外部函數的變量 # a = 1 # def outer(): # a = 1 # def inner(): # a = 2 # def inner2(): # nonlocal a #聲明瞭一個上面第一層局部變量 # a += 1 #不可變數據類型的修改 # inner2() # print('##a## : ', a) # inner() # print('**a** : ',a) # outer() # print('全局 :',a)
關於nonlocal 聲明函數
# nonlocal 只能用於局部變量 找上層中離當前函數最近一層的局部變量 # 聲明瞭nonlocal的內部函數的變量修改會影響到 離當前函數最近一層的局部變量 # 對全局無效 # 對局部 也只是對 最近的 一層 有影響 # a = 0 # def outer(): # # a = 1 # def inner(): # # a = 2 # def inner2(): # nonlocal a # print(a) # inner2() # inner() # outer()