I have the following Python script:
from contextlib import contextmanager
@contextmanager
def my_content_manager():
self.entrance = True
try:
yield
except Exception as ex:
self.entrance = False
with my_content_manager() as cm:
print (cm.entrance)
print (cm.entrance)
When I tried to run the script, I got the following:
Traceback (most recent call last):
File "test.py", line 12, in <module>
with my_content_manager() as cm:
File "C:\Users\abc\AppData\Local\Programs\Python\Python36\lib\contextlib.py", line 82, in __enter__
return next(self.gen)
File "test.py", line 5, in my_content_manager
self.entrance = True
NameError: name 'self' is not defined
Why is that? How can I solve this error?
Thanks.