请问func()在函数中使用时在python中是什么意思
请了解在函数中使用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 这是一些关于装饰
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
您可以看到装饰函数未被调用。请看这里虽然公认的答案在技术上是准确的,但这更有帮助。谢谢你的例子和链接。