我正在学习装饰器设计模式教程 (归功于 Jungwoo Ryoo)
我很好奇为什么我可以换行:return decorator
和 print(hello_world())
with return decorator()
和 print(hello_world)
from functools import wraps
def make_blink(function):
"""Defines the decorator"""
@wraps(function)
# Define the inner function
def decorator():
# Grab the return value of the function being decorated
ret = function()
# Add new functionality to the function being decorated
return "<blink>"+ ret + "<b/link>"
return decorator #return decorator()#<THIS LINE HERE SWAPPED
# Apply the decorator here!
@make_blink
def hello_world():
"""Original function! """
return "Hello, World!"
# Check the result of decorating
print(hello_world()) #print(hello_world) #<THIS LINE HERE SWAPPED
解释器不会每次都做不同的事情吗?我只是在寻找一些见解,以便更好地了解正在发生的事情
最佳答案
装饰器实际上只是函数,而函数只是对象。
线条
@make_blink
def hello_world():
# ...
本质上是一样的
def hello_world():
# ...
hello_world = make_blink(hello_world)
除了函数对象永远不会首先分配给 hello_world
(它在堆栈中以传递给装饰器)。
因此,无论您从 make_blink()
返回什么,都会分配回 hello_world
。这可以是一个函数对象,但也可以是完全不同的东西。
因此,当您使用return decorator
时,您告诉Python 将hello_world
设置为嵌套函数对象。当您使用 return decorator()
时,您告诉 Python 使用 decorator()
函数的结果。在这里,这是一个字符串值。就好像你这样做了:
def hello_world():
"""Original function! """
return "Hello, World!"
hello_world = "<blink>" + hello_world() + "</blink>"
这对于这个特定示例来说很好,因为 hello_world()
函数的主体每次只返回相同的字符串。
但是,如果您将原始的 hello_world()
函数体更改为每次调用时都返回不同的内容,会怎样呢?如果你有:
import random
@make_blink
def random_greeting():
return 'Hello ' + random.choice('DonAr', 'Martijn Pieters', 'Guido van Rossum') + '!'
现在 big 您从 make_blink()
调用返回的内容有很大不同!对于模块的顶层,装饰器仅在导入时执行一次。如果您使用 return decorator()
,您将只运行一次 random.choice()
,并且您已经固定 的值random_greeting
到单个静态字符串结果。
一般来说,装饰器都希望再次返回一个可调用对象。这可以是原始函数(装饰器只是更新某种注册)、包装函数(在调用原始函数之前或之后做额外的事情),甚至是完全不同的东西。但这并不是一成不变的,解释器也不关心这两种情况。
装饰器只是可在您的程序中使用的东西,一种工具。如果您对返回原始函数结果的装饰器有特定用途,那么您可以随意使用。
关于python - 很好奇解释器在这里做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52247395/