In Context Managers:
Although not a common practice, else can be used in conjunction with context managers to execute code based on the successful completion of the context block.
class CustomContextManager:
def __enter__(self):
print("Entering context")
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
print("Exiting context successfully")
else:
print("Exiting context with exception:", exc_type)
with CustomContextManager():
print("Inside context block")
#clcoding.com
Entering context
Inside context block
Exiting context successfully
0 Comments:
Post a Comment