Python 3.7
Т.е. сделать так, чтобы функция перестала выполнять свою задачу.
def test():
print('blah_blah_blah')
test()
del test
test()
>>>
blah_blah_blah
Traceback (most recent call last):
File "<модуль1>", line 6, in <module>
NameError: name 'test' is not defined
>>>
def test(): print('blah_blah_blah') def other_func(): print('ooops...') test() test = other_func test() >>> blah_blah_blah ooops... >>>
class disable(object): def __init__(self, func): self.func = func self.active = True def __call__(self, *args, **kwargs): if self.active: return self.func( *args, **kwargs) @disable def foo(): print 'i am working' foo() foo() foo.active = False foo() foo() foo.active = True foo() foo()