【发布时间】:2019-02-14 06:35:22
【问题描述】:
我正在学习装饰器设计模式教程 (感谢 Jungwoo Ryoo)
我很好奇为什么我可以换行:return decorator
和print(hello_world()) 和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
口译员不是每次都在做不同的事情吗?我只是在寻找一些见解,以便更好地了解正在发生的事情
【问题讨论】:
标签: python python-3.x python-decorators