剛才那些就是函數(shù)的基本知識了。我們來讓你的知識更進一步。在Python中我們可以在一個函數(shù)中定義另一個函數(shù):
def hi(name="yasoob"):
print("now you are inside the hi() function")
def greet():
return "now you are in the greet() function"
def welcome():
return "now you are in the welcome() function"
print(greet())
print(welcome())
print("now you are back in the hi() function")
hi()
#output:now you are inside the hi() function
# now you are in the greet() function
# now you are in the welcome() function
# now you are back in the hi() function
# 上面展示了無論何時你調(diào)用hi(), greet()和welcome()將會同時被調(diào)用。
# 然后greet()和welcome()函數(shù)在hi()函數(shù)之外是不能訪問的,比如:
greet()
#outputs: NameError: name 'greet' is not defined
那現(xiàn)在我們知道了可以在函數(shù)中定義另外的函數(shù)。也就是說:我們可以創(chuàng)建嵌套的函數(shù)?,F(xiàn)在你需要再多學一點,就是函數(shù)也能返回函數(shù)。