代码之家  ›  专栏  ›  技术社区  ›  Aditya

类装饰器在Python2.7中没有按预期工作

  •  0
  • Aditya  · 技术社区  · 4 年前

    def supress_help(cls, a, b):
        def wrapper(func):
            def wrappy(*arg, **kwargs):
                if arg[1] < 5:
                    func(arg[1])
                else:
                    print("Not calling the helper")
            return wrappy
    
        cls.help = wrapper(cls.help)
        print(a + b)
        return cls
    
    @supress_help(a=7, b="Hello")
    class Helper(object):
    
        def help(x):
            print(x)
    
    if __name__ == "__main__":
        obj = Helper()
        obj.help(2)
        obj.help(7)
    
    
    
    Traceback (most recent call last):
      File "/home/aditya/class_decorator.py", line 14, in <module>
        @supress_help(a=7, b="Hello")
    TypeError: supress_help() takes at least 1 argument (2 given)
    
    0 回复  |  直到 4 年前