鱼C论坛

 找回密码
 立即注册
查看: 1690|回复: 3

[已解决]把函数B作为参数传入函数A 同时B里还有参数 是不行的吗?

[复制链接]
发表于 2023-6-25 12:34:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import time
def time_master(func):
    print('Starting...')
    start = time.time()
    func()
    stop = time.time()
    print('Finish')
    print(f'Spend {(stop - start):.2f} second.')
def myfunc(y):
    time.sleep(0.5)
    x = 1
    for i in range(1,y):
        x *= i
    print(x)
time_master(myfunc(10))  #外层函数计算内层函数所用时间 内层函数计算阶乘 参数为y  这样搞不行吗?
最佳答案
2023-6-25 12:59:23
这样是不行的,因为你现在是直接调用了 myfunc(10) ,而不是将它作为一个参数传递给 time_master 函数。 myfunc(10) 的返回值是 None ,所以你实际上是在调用 time_master(None) 。

正确的做法是将函数名 myfunc (不带括号和参数)作为参数传递给 time_master 函数,同时传递需要的参数。你可以将 myfunc 的参数包装在一个函数中,然后将这个包装函数作为参数传递给 time_master 函数,如下所示:
import time
def time_master(func):
    print('Starting...')
    start = time.time()
    func()
    stop = time.time()
    print('Finish')
    print(f'Spend {(stop - start):.2f} second.')

def myfunc(y):
    time.sleep(0.5)
    x = 1
    for i in range(1,y):
        x *= i
    print(x)

def wrapper():
    myfunc(10)

time_master(wrapper)  #这样就可以了

或者,你也可以修改 time_master 函数,让它接受函数及其参数作为参数,如下所示:
import time
def time_master(func, *args, **kwargs):
    print('Starting...')
    start = time.time()
    func(*args, **kwargs)
    stop = time.time()
    print('Finish')
    print(f'Spend {(stop - start):.2f} second.')

def myfunc(y):
    time.sleep(0.5)
    x = 1
    for i in range(1,y):
        x *= i
    print(x)

time_master(myfunc, 10)  #这样也可以
函数做参数还是有参数.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-25 12:59:23 | 显示全部楼层    本楼为最佳答案   
这样是不行的,因为你现在是直接调用了 myfunc(10) ,而不是将它作为一个参数传递给 time_master 函数。 myfunc(10) 的返回值是 None ,所以你实际上是在调用 time_master(None) 。

正确的做法是将函数名 myfunc (不带括号和参数)作为参数传递给 time_master 函数,同时传递需要的参数。你可以将 myfunc 的参数包装在一个函数中,然后将这个包装函数作为参数传递给 time_master 函数,如下所示:
import time
def time_master(func):
    print('Starting...')
    start = time.time()
    func()
    stop = time.time()
    print('Finish')
    print(f'Spend {(stop - start):.2f} second.')

def myfunc(y):
    time.sleep(0.5)
    x = 1
    for i in range(1,y):
        x *= i
    print(x)

def wrapper():
    myfunc(10)

time_master(wrapper)  #这样就可以了

或者,你也可以修改 time_master 函数,让它接受函数及其参数作为参数,如下所示:
import time
def time_master(func, *args, **kwargs):
    print('Starting...')
    start = time.time()
    func(*args, **kwargs)
    stop = time.time()
    print('Finish')
    print(f'Spend {(stop - start):.2f} second.')

def myfunc(y):
    time.sleep(0.5)
    x = 1
    for i in range(1,y):
        x *= i
    print(x)

time_master(myfunc, 10)  #这样也可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-25 13:41:30 | 显示全部楼层
isdkz 发表于 2023-6-25 12:59
这样是不行的,因为你现在是直接调用了 myfunc(10) ,而不是将它作为一个参数传递给 time_master 函数。 my ...

就是 参数套娃是无法实现的咯
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-25 13:43:06 | 显示全部楼层
mumei2018 发表于 2023-6-25 13:41
就是 参数套娃是无法实现的咯

不完全是的。在Python中,函数是一级对象,这意味着你可以把函数当作参数传给另一个函数,或者将它作为另一个函数的返回值。但是,你需要正确地做这件事。

当你试图像这样调用 time_master(myfunc(10)) 时,实际上在传递 myfunc(10) 的返回值(也就是None)给 time_master ,而不是函数 myfunc 本身。Python首先会求解括号中的表达式 myfunc(10) ,然后将其结果传递给 time_master 函数。

如果你想将一个带有参数的函数作为参数传递给另一个函数,有几种方法可以实现:

1. 将带参数的函数包装在一个无参数的函数中,然后将无参数的函数传递给另一个函数。这就是我在上一个回答中展示的 wrapper 函数的作用。

2. 修改接受函数作为参数的函数(即 time_master 函数),让它接受一个函数和一系列参数,然后在 time_master 函数内部调用这个函数。这就是我在上一个回答中的第二个示例所做的。

3. 使用偏函数( functools.partial )或lambda函数来创建一个无需额外输入参数的新函数。例如,你可以创建一个新的函数 myfunc_with_10 = lambda: myfunc(10) ,然后将 myfunc_with_10 作为参数传递给 time_master 函数。

总的来说,你可以将带有参数的函数作为参数传递给另一个函数,但你需要使用适当的方式来实现这一点。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 17:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表