对装饰者有很大疑虑。 在简单的测试代码下面,请阅读一些初学者的书。
# -*-coding:Latin-1 -*
import sys
# The decorator.
def my_decorator(modified_function):
"""My first decorator."""
# Modifying modified_function.
def modifying_function():
print("--> before")
ret = modified_function()
print("<-- after={}".format(ret))
return ret
print("Decorator is called with modified_function '{0}'".format(modified_function))
# Return the modifying function.
return modifying_function
@my_decorator
def hello_world():
"""Decorated function."""
print("!! That's all folks !!")
return (14)
print("Python version = {}".format(sys.version))
# We try to call hello_world(), but the decorator is called.
hello_world()
print("--------------------------------------------------------------")
my_decorator(hello_world)
print("--------------------------------------------------------------")
# Found this other way on the WEB, but does not work for me
my_hello = my_decorator(hello_world)
my_hello()
print("--------------------------------------------------------------")
对于这段代码,输出对我来说很奇怪。 也许这很愚蠢,但是......
Decorator is called with modified_function '<function hello_world at 0x0000011D5FDCDEA0>'
Python version = 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]
--> before
!! That's all folks !!
<-- after=14
--------------------------------------------------------------
Decorator is called with modified_function '<function my_decorator.<locals>.modifying_function at 0x0000011D5FDCDF28>'
--------------------------------------------------------------
Decorator is called with modified_function '<function my_decorator.<locals>.modifying_function at 0x0000011D5FDCDF28>'
--> before
--> before
!! That's all folks !!
<-- after=14
<-- after=14
--------------------------------------------------------------
欢迎对新手进行任何澄清。
答案 0 :(得分:0)
您的混淆与您手动使用装饰器的事实有关,但您还使用Python的特殊语法将装饰器应用于定义它们的函数: @my_decorator
。你通常不想为同一个功能做这两件事。
试试这个例子,它可以帮助你更好地理解事情:
# after defining my_decorator as before...
def foo(): # define a function for testing with manual decorator application
print("foo")
print("----")
foo() # test unmodified function
print("----")
decorated_foo = my_decorator(foo) # manually apply the decorator (assigning to a new name)
print("----")
decorated_foo() # test decorated function
print("----")
foo() # confirm that the original function is still the same
print("----")
foo = my_decorator(foo) # apply the decorator again, replacing the original name this time
print("----")
foo() # see that the original function has been replaced
print("----")
@my_decorator # this line applies the decorator for you (like the "foo = ..." line above)
def bar():
print("bar")
print("----")
bar() # note that the decorator has already been applied to bar
print("----")
double_decorated_bar = my_decorator(bar) # apply the decorator again, just for fun
print("----")
double_decorated_bar() # get doubled output from the doubled decorators
答案 1 :(得分:0)
哦......好的。 我看到光明来了:
double_decorated_bar = my_decorator(bar) # apply the decorator again, just for fun
没注意@decorator在示例中不存在。 再次澄清。