请问在函数内部使用时func()在python中是什么意思,例如在下面的代码中.
def identity_decorator(func):
def wrapper():
func()
return wrapper
Run Code Online (Sandbox Code Playgroud)
我也在想同样的问题!您可以通过以下示例了解它是如何工作的:
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
Run Code Online (Sandbox Code Playgroud)
现在,当您删除 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
Run Code Online (Sandbox Code Playgroud)
你看到装饰的函数没有被调用。请看这里https://www.programiz.com/python-programming/decorator
| 归档时间: |
|
| 查看次数: |
9577 次 |
| 最近记录: |