Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Read about context managers. What is it, how to work with it.
As a sandbox, you can start with this code:
class func:
def __init__(self):
self.condition = 'вне контекста'
# функция описывает методы, вызываемые при старте контекстного менеджера
# Возвращаемое значение уходит в переменную var в конструкции 'with ... as var:'
def __enter__(self):
self.condition = 'в контексте'
return self
# Метод вызываемый в завершении конструкции with или при ошибке после нее
def __exit__(self, type, value, traceback):
pass
def __call__(self):
print(f'Выполнение функции {self.condition}')
out_context = func()
out_context()
print('-'*40)
with func() as inside_context:
inside_context()
Выполнение функции вне контекста
----------------------------------------
Выполнение функции в контексте
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question