python基础---函数整体作为实参被传入另外一个函数会怎样?

def do_ntimes(fuct,n=1):
    for i in range(n):
        print(i)

do_ntimes(print('help me!'),5)

输出 :

help me!
0
1
2
3
4

过程是

1 do_ntimes()函数开始运行 得到这个函数的地址,然后开始接受参数 

2 func参数的实参是一个函数 发现这一点后运行一遍这个函数,得到返回值 (第一个 help me !就是这个来的)

3运行for循环  

之所以后面除了下标 啥都没有是因为函数的返回值是None

想达到效果可以这样 

def test():return 'help  me !'

def do_ntimes(fuct,n=1):
    for i in range(n):
        print(fuct)

do_ntimes(test(),5)

help  me !
help  me !
help  me !
help  me !
help  me !
原文地址:https://www.cnblogs.com/yuanji2018/p/9145865.html