当然,你可以这样做:
def italic(predecessor): x = predecessor def successor(): return "" + x() + "" return successor
就像你能做的那样:
def italic(predecessor): x = predecessor x2 = x def successor(): return "" + x2() + "" return successor
要么
def italic(predecessor): x = predecessor x2 = x x3 = x2 def successor(): return "" + x3() + "" return successor
但你为什么要这样做?
编写装饰器的最佳方法是functools.wraps
:
from functools import wraps def italic(f): @wraps(f) def decorated(): return "" + f() + "" return decorated
但如果你真的想要与众不同:
import functools def italic(f): return functools.update_wrapper(lambda: "" + f() + "", f)