python - 调用函数并打印函数名称

标签 python function loops for-loop

我有一个很长的函数列表,我希望能够调用它们并让它们打印出它们的名字以及它们何时完成,如下所示。

def call(*functions):
    for f in functions:
        print(f.__name__)
        f()
        print('{} completed'.format(f.__name__))

call(lambda: (long(), lst(), of(), func(), ions()...))

我不想在每个函数周围编写 print(f.__name__) ... print('{}completed'.format(f.__name__)) 。然而,在上面的代码中它打印“lambda”(如预期)。如何自动执行这些函数调用/打印语句以便正确打印?

最佳答案

def call(*functions):
    for f in functions:
        print(f.__name__)
        f()
        print('{} completed'.format(f.__name__))

call(long, lst, of, func, ions...)

但是,使用 map 会更符合逻辑。并简化调用:

def call(function):
    print(f.__name__)
    f()
    print('{} completed'.format(f.__name__))

map(call, (long, lst, of, func, ions...))

关于python - 调用函数并打印函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488605/

相关文章:

c - 有没有办法在 C 中循环遍历具有不同类型元素的结构?

python - 将点添加到现有的 matplotlib 散点图

python - Google Cloud SQL - 操作系统环境未设置为 Google App Engine

python - 在 Pelican 静态站点生成器 ATOM 提要中使用绝对 URL

javascript - 原型(prototype)与函数定义

c++ - Profiler 表明函数调用开销是正常语句的 10 倍

php - 选中时从表中插入多个数组项

python - 使用循环在 .ini 文件中分配已知数量的变量

python - 我怎样才能改变我的Python代码,以便它可以更有效地将txt文件转换为CSV?

javascript - 将另一个函数添加到 onclick