Get class in Python decorator
在本代码中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def online_only(func, self): def f(*args, **kwargs): if self.running: return func(*args, **kwargs) else: return False return f class VM(object): @property def running(self): return True @property @online_only def diskinfo(self): return True |
我只希望在vm.running返回true时运行diskinfo。我怎样才能上网只阅读自我跑步?
将
1 2 3 4 5 6 7 | def online_only(func): def f(self, *args, **kwargs): if self.running: return func(self, *args, **kwargs) else: return False return f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def online_only(func): def f(self): if self.running: return func(self) else: return False return f class VM(object): @property def running(self): return True @property @online_only def diskinfo(self): return True print VM().diskinfo |