保利cc学Python 发表于 2023-2-13 23:04:33

python函数问题求解答

这里面的func()什么作用   与下面函数中的funB()有什么区别

import time   
def time_master(func):
    def call_func():
      print("开始运行程序...")
      start = time.time()
      func()
      stop = time.time()
      print("结束程序运行...")
      print(f"一共耗费了 {(stop-start):.2f} 秒。")
    return call_func
   
@time_master
def myfunc():
    time.sleep(2)
    print("I love FishC.")
   
myfunc()



>>> def funA():
...   x = 520
...   def funB():
...         x = 880
...         print("In funB, x =", x)
...   funB()
...   print("In funA, x =", x)
...
>>> funA()
In funB, x = 880
In funA, x = 520

wp231957 发表于 2023-2-14 08:37:21

func 不是传进去的参数吗   这个参数应该是一个函数地址
所以func() 就是函数调用

chinajz 发表于 2023-2-14 12:43:00

@time_master
def myfunc():
    time.sleep(2)
    print("I love FishC.")
#@time_master相当于time_master(myfunc)
页: [1]
查看完整版本: python函数问题求解答