От: | Ватакуси | ||
Дата: | 02.07.24 17:50 | ||
Оценка: | 53 (2) |
@contextmanager
def needRun():
try:
yield
except RuntimeError as e:
if e.args[0] != "generator didn't yield":
raise # Raising everything else
@contextmanager
def contextOnCondition(flag: bool=True):
if flag:
yield True
if __name__ == "__main__":
flag = True
with needRun(), contextOnCondition(flag):
print("It ran")
flag = False
with needRun(), contextOnCondition(flag):
print("It did not run")
Б>