There is no real need to implement this decorator as a class, and there is no need to implement it in the definition of the Foo class. The following is enough:
def canRun(meth): def decorated_meth(self, *args, **kwargs): if self.longcondition(): print 'Can run' return meth(self, *args, **kwargs) else: print 'Cannot run' return None return decorated_meth
Using this decorator works:
>>> Foo('hello').run(5) Can run 0. run... hello 1. run... hello 2. run... hello 3. run... hello 4. run... hello >>> Foo(123).run(5) Cannot run
source share