2
考慮下面來裝飾裝飾器是不同於裝飾器的功能類嗎?
class MethodDecoratorC(object):
def __init__(self,func):
self.func = func
def __call__(self,*args,**kwargs):
print(len(args))
print(len(kwargs))
self.func(*args,**kwargs)
def method_decorator_f(func):
def wrapped_func(*args,**kwargs):
print(len(args))
print(len(kwargs))
func(*args,**kwargs)
return wrapped_func
他們期待着做同樣的,和功能這是真的:
@MethodDecoratorC
def test_method_c(a):
print(a)
@method_decorator_f
def test_method_f(a):
print(a)
test_method_f("Hello World! f")
test_method_c("Hello World! c")
打印:
1
0
Hello World! f
1
0
Hello World! c
對於方法然而,有些東西很奇怪的事情發生:
class TestClass(object):
@MethodDecoratorC
def test_method_c(self,a):
print(a)
@method_decorator_f
def test_method_f(self,a):
print(a)
t = TestClass()
t.test_method_f("Hello World! f")
t.test_method_c("Hello World! c")
個
打印:
2
0
Hello World! f
1
0
Traceback (most recent call last):
File "test5.py", line 40, in <module>
t.test_method_c("Hello World! c")
File "test5.py", line 8, in __call__
self.func(*args,**kwargs)
TypeError: test_method_c() takes exactly 2 arguments (1 given)
沒有太多的期待!不知何故,TestClass對象不會作爲參數傳遞給裝飾器對象的__call__
方法。
爲什麼這種差異?還有,我仍然可以在我的類風格裝飾器中獲取對象嗎?