Python返回內(nèi)兩層函數(shù)
def f():
? ?print('call f()...')
? ?# 定義函數(shù)g:
? ?def g():
? ? ? ?print('call g()...')
? ? ? ?# 定義函數(shù)h:
? ? ? ?def h():
? ? ? ? ? ?print('call h()...')
? ? ? ?# 返回函數(shù)h:
? ? ? ?return h
? ?# 返回函數(shù)g:
? ?return g
x = f()
print(x)
y = x()
print(y)
z = y()
print(z)
>>> <function f.<locals>.g at 0x0000020E584749A0>
>>>?call g()...
>>>?<function f.<locals>.g.<locals>.h at 0x0000020E58474F40>
>>>?call h()...
>>>?None
為什么z作為返回函數(shù)h的對象,卻沒有地址呢?
2025-01-06
z=y()? 調(diào)用的 最里面的h()函數(shù),該函數(shù)沒有返回值,所以z是None
def?h():
? ? ? ? ? ?print('call h()...')