Get class in Python decorator

后端 未结 2 1864
广开言路
广开言路 2020-12-17 04:54

In this code:

def online_only(func, self):
    def f(*args, **kwargs):
        if self.running:
            return func(*args, **kwargs)
        else:
               


        
2条回答
  •  借酒劲吻你
    2020-12-17 05:30

    self is passed as the first parameter to the wrapping function, so just treat the first parameter specially in f:

    def online_only(func):
        def f(self, *args, **kwargs):
            if self.running:
                return func(self, *args, **kwargs)
            else:
                return False
        return f
    

提交回复
热议问题