第一种情况:在调用second方法的时候,参数名为second
def first(a, b): print "asdf",a b() return "bbb" def second(): print "success" return "ok" print first("aaa", second)
输出结果为
asdf aaa
success
bbb
第二种情况:参数名为second()
def first(a, b): print "asdf",a return "bbb" def second(): print "success" return "ok" print first("aaa", second())输出结果为:
success
asdf aaa
bbb
综合这两种情况,可以发现,在传参为函数名字的时候,会在调用的位置执行该传入的函数;在传参为函数名加括号的时候,会优先执行该传参的函数
第三种情况:second有参数的,如果直接在调用的时候用first("aaa", second("fff")),会优先执行second,所以不能这样
def first(a, b, f): print "asdf",a b(f) return "bbb" def second(c): print "success" print "second",c return "ok"
print first("aaa", second)
输出结果为
asdf aaa
success
second ffff
bbb
这样就完美多了
第四种情况:只不过是个演化
def first(a, b, *arg): print "asdf",a b(*arg) return "bbb" def second(c, d): print "success" print "second",c print "second",d return "ok"
print first("aaa", second, "tttt", "fffff")
对于多参数的传入
asdf aaa
success
second tttt
second fffff
bbb