Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请问func()在函数中使用时在python中是什么意思_Python_Python 2.7 - Fatal编程技术网

请问func()在函数中使用时在python中是什么意思

请问func()在函数中使用时在python中是什么意思,python,python-2.7,Python,Python 2.7,请了解在函数中使用func()时在python中的含义,例如在下面的代码中 def identity_decorator(func): def wrapper(): func() return wrapper func是函数identity\u decorator()的参数 表达式func()表示“调用分配给变量func的函数” decorator将另一个函数作为参数,并返回一个新函数(定义为wrapper),该函数在运行时执行给定函数func 这是一些关于装饰

请了解在函数中使用func()时在python中的含义,例如在下面的代码中

def identity_decorator(func):
    def wrapper():
        func()
    return wrapper

func
是函数
identity\u decorator()
的参数

表达式
func()
表示“调用分配给变量
func
的函数”

decorator将另一个函数作为参数,并返回一个新函数(定义为
wrapper
),该函数在运行时执行给定函数
func


这是一些关于装饰师的信息。

我也在想同样的问题!您可以通过以下示例了解其工作原理:

def make_pretty(func):
    def inner():
       print("I got decorated")
       func()
    return inner

def ordinary():
    print("I am ordinary")

pretty = make_pretty(ordinary)
pretty()

Output
I got decorated
I am ordinary 
现在,当您删除func()并尝试重新运行它时:

def make_pretty(func):
    def inner():
       print("I got decorated")
    return inner

def ordinary():
    print("I am ordinary")

pretty = make_pretty(ordinary)
pretty()

Output
I got decorated

您可以看到装饰函数未被调用。请看这里

虽然公认的答案在技术上是准确的,但这更有帮助。谢谢你的例子和链接。