當我一個解釋會話中運行的執行順序與打印功能調用的函數的參數
def func(one,two):
print(one)
two
func("1",print("hi"))
func("2",print("hello"))
困惑,我得到的輸出:
hi
1
hello
2
是不應該的代碼從上到下跑?我期望的輸出是:
1
hi
2
hello
當我一個解釋會話中運行的執行順序與打印功能調用的函數的參數
def func(one,two):
print(one)
two
func("1",print("hi"))
func("2",print("hello"))
困惑,我得到的輸出:
hi
1
hello
2
是不應該的代碼從上到下跑?我期望的輸出是:
1
hi
2
hello
的print
語句返回None
,它要傳遞到你的函數作爲第二個參數。
當您撥打func("1", print("hi"))
時會發生什麼,print("hi")
將被執行,因此您將首先看到hi
。 之後,func("1", None)
被調用,它會做
print("1")
None
,你會從看到的唯一輸出爲1
,因爲語句
None
什麼都不做。
當你調用一個函數時,參數首先被評估。因此「hi」已經打印。當評估參數時,它們被傳遞給該函數,並調用打印「1」的函數。