2016-07-26 43 views
1

我想在定义装饰器时禁止Eclipse警告。禁止在使用Pydev进行开发时使用Eclipse

例如:

def tool_wrapper(func): 
    def inner(self): 
     cmd="test" 
     cmd+=func(self) 
    return inner 

@tool_wrapper 
def list_peer(self): 
    return "testing " 

我得到一个装饰定义警告: “方法‘tool_wrapper’应该有自身作为第一个参数

我定义一个类里面的装饰,所以这是只有这样,它的正常工作。

感谢

+0

您定义的类中的装饰?为什么? –

回答

1

只要定义你的装饰类外,并传递实例作为参数,它会工作得很好。

def tool_wrapper(func): 
    def inner(inst): # inst : instance of the object 
     cmd="test" 
     cmd+=func(inst) 
     return cmd 
    return inner 


class Test(): 

    def __init__(self): 
     pass 

    @tool_wrapper 
    def list_peer(self): 
     return "testing " 


if __name__ == '__main__': 
    t = Test() 
    print t.list_peer() 

此脚本会打印testtesting

+0

谢谢,它工作得很好!它是否正确使用模块文件中类之外的函数的Python语法? –

+0

我想是的,只要你进行必要的进口 – BusyAnt

相关问题