我目前正在學習python,使用「Think Python」,其中我通過下面的某段代碼進行學習,並且我是一名初學者程序員,我不明白它是如何工作的,請解釋一下下面的代碼以及它背後的各種概念。與Python中函數對象的使用相混淆
excercise:函數對象是一個值,你可以賦值給變量或作爲參數傳遞。對於 例如,do_twice是一個函數,函數對象作爲參數,並調用它兩次:
def do_twice(f):
f()
f()
# Here’s an example that uses do_twice to call a function named print_spam twice.
def print_spam():
print 'spam'
do_twice(print_spam)
這個代碼給出了O/P爲 垃圾 垃圾 我不知道我怎麼和想更深入的解釋這個概念
你的問題是什麼?你有什麼不明白? – BrenBarn