【发布时间】:2017-05-23 18:29:01
【问题描述】:
装饰函数时,可以使用@object.method的方法,也可以使用@object.attribute.attribute.method等属性属性的方法。你也可以将额外的参数传递给装饰器@function(foo="bar")。
但是,看来这些冲突。当链中有函数调用时,python 似乎假定它是您将参数传递给装饰器的位,并且之后的任何链都是 SyntaxError。
这里有什么我遗漏的吗?这种行为的原因或解决方法?
此代码是为 Python 3.4 编写的。
#!/usr/bin/env python3
class Decorator:
def decorate(self, callback):
return callback
_dec = Decorator()
def findit():
return _dec
class B: dec = _dec
class A: bar = B()
foo = A()
dec = findit()
@dec.decorate
#@findit().decorate
#Above line is a syntax error
@foo.bar.dec.decorate #also permitted
def function():
pass
错误:
File "test.py", line 17
@findit().decorate
^
SyntaxError: invalid syntax
【问题讨论】:
-
试试
@(findit().decorate)。可能是优先问题? -
@(findit().decorate)在左括号上出现语法错误。