# 1函式的巢狀呼叫
defbar()
:print
('from bar'
)def
foo():
print
('from foo'
) bar(
)foo(
)# 示例
defmax2
(x, y)
:if x > y:
return x
else
:return y
defmax4
(a, b, c, d)
: res1 = max2(a, b)
res2 = max2(c, res1)
res3 = max2(res2, d)
print
(res3)
max4(13,
414,
524,
525)
# 2函式的巢狀定義
deff1()
:print
('from f1'
)deff2(
):# 函式內可以定義函式
print
('from f2'
)print
(f2)
f2()# 函式內可以呼叫函式
f1()
# 定義在函式內的函式特點是:正常情況只能在函式體內呼叫
from math import pi
defcircle
(r, mode=0)
:def
perimeter
(r):
return
2* pi * r
defarea
(r):
return pi *
(r **2)
if mode ==0:
return perimeter(r)
elif mode ==1:
return area(r)
res1 = circle(3,
0)print
(f'perimeter is '
)res2 = circle(3,
1)print
(f'area is '
)
函式的巢狀
例1 def index print from index def func index print from func func 定義乙個index函式 定義乙個func函式 呼叫func函式,然後跳轉到那邊執行語句,先執行index 語句,列印from index,然後往下執行,列印from f...
函式的巢狀
函式的巢狀定義 def f1 函式定義完畢,通過f1 呼叫函式,函式體開始執行 def f2 先找到f3 並不會立刻就去找呼叫,在同級裡一步一步往下走,先到print from f2 print from f2 再到呼叫,最後執行f3 函式體 def f3 print lucas print fro...
函式的巢狀
a 1 def outer a 2 def inner a 3 def inner2 nonlocal a nonlocal只能用於區域性變數,找最近一層的區域性變數 a 1 inner2 print inner a a inner print a a outer print a def func ...