Здравствуйте, Буравчик, Вы писали:
Б>Нужен именно контекстный менеджер. И поэтому приходится писать:
Б>Б>with manager() as m:
Б> if m.condition:
Б> ... code here ...
Б>
Надо больше угара
class Manager:
def __init__(self):
self.active=True
self.last_fn=None
def need_run(self,fn,scope):
print("run",scope,self.active)
return self.active
def exec_with(manager,scope=""):
def decorator(fn):
manager.last_fn=fn
if manager.need_run(fn,scope): fn(manager)
return decorator
manager=Manager()
#---------------------------------------------------
@exec_with(manager,"main")
def body(m):
print("code here 1")
manager.active=False
@exec_with(manager,"optional")
def _(m):
print("code here 2")