小编use*_*484的帖子

Python装饰器@func().属性语法错误

我试图在这里找到答案,但不能.

@obj.func # works
@obj.func(**kwargs)  #works
@obj.func1(**kwargs).func2   #-> syntax error 
Run Code Online (Sandbox Code Playgroud)

我不明白为什么第三种形式是SyntaxError,对我来说似乎没有违反任何python语法,我很清楚用户想要做什么(见下面的例子).

我看了装饰器实现的pep 0318,但没有找到任何答案.

下面,这将是一个使用的例子:

class ItemFunc(object):
    def __init__(self, fcall=None, **kwargs):
        self.defaults = kwargs
        self.fcall = None

    def __call__(self, *args, **kwargs):
        kwargs = dict(self.defaults, **kwargs)
        # do something more complex with kwargs 
        output = self.fcall(*args, **kwargs)
        # do something more with output  
        return output

    def caller(self, fcall):
        """ set call and return self """
        self.call = fcall # after some check obviously
        return self

    def copy(self,**kwargs):
        kwargs = dict(self.defaults, …
Run Code Online (Sandbox Code Playgroud)

python attributes class decorator python-decorators

5
推荐指数
1
解决办法
493
查看次数

标签 统计

attributes ×1

class ×1

decorator ×1

python ×1

python-decorators ×1