使用装饰器练习,发现这种行为很奇怪:
def test_decorator(cls, *args, **kwargs):
print (args, kwargs)
def build(*args, **kwargs):
print (args, kwargs)
return cls(*args, **kwargs)
return build
@test_decorator
class Test:
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
t = Test(1, 2, 3, val = 4)
print (t.args, t.kwargs)
# output
# () { }
# (1, 2, 3) {'val' = 4}
# (1, 2, 3) {'val' = 4}
为什么装饰器中的第一个print
显示空容器?此外,如果我将build()
定义为:
def build():
return cls(*args, **kwargs)
我知道它会因nested function scoping而失败。我只是不确定为什么他们在调用build
之前以某种方式不存在并突然返回范围。
答案 0 :(得分:4)
这里有两个不同的callables:
test_decorator()
test_decorator()
,build()
返回的包装器。你这两个人很困惑。
第一个是使用仅调用类,因为
@test_decorator
class Test:
# ...
真的只是
class Test:
# ...
Test = test_decorator(Test)
该调用仅传递一个参数,即正在修饰的类,该类被分配给cls
名称。该调用的args
和kwargs
参数保持为空。
当您再拨打Test(...)
时,您实际上正在呼叫build(...)
。该调用是由本地args
和kwargs
对象捕获的传递参数,并传递给cls(...)
(引用原始类对象)。这些参数不会丢失,它们显然会被传递给__init__
方法,并且正确设置了相同名称的实例属性。
要区分不同的catch-all参数,首先给它们指定不同的名称并增加print()
输出:
def test_decorator(cls, *decorator_args, **decorator_kwargs):
print('Decorator called with ({!r}, *{!r}, **{!r})'.format(
cls, decorator_args, decorator_kwargs))
def build(*build_args, **build_kwargs):
print('build() wrapper called with (*{!r}, **{!r})'.format(
build_args, build_kwargs))
print('The decorator was originally called with ({!r}, *{!r}, **{!r})'.format(
cls, decorator_args, decorator_kwargs))
return cls(*build_args, **build_kwargs)
return build
现在输出变为:
>>> @test_decorator
... class Test:
... def __init__(self, *args, **kwargs):
... self.args = args
... self.kwargs = kwargs
...
Decorator called with (<class '__main__.Test'>, *(), **{})
>>> t = Test(1, 2, 3, val = 4)
build() wrapper called with (*(1, 2, 3), **{'val': 4})
The decorator was originally called with (<class '__main__.Test'>, *(), **{})
>>> t.args, t.kwargs
((1, 2, 3), {'val': 4})
请注意,在执行class
语句时生成了使用... 输出调用的 Decorator,而t = Test(...)
调用触发了 build()包装叫...... 输出。