Given the following ContextManager classes, here is the order of __exit__ calls in the following example - reverse order of declaration.
Interestingly, __exit__ can be called multiple times. Is this deterministic in all versions of Python from 2.7? If not, from which version is this order reliably deterministic?
a = A()
b = B()
c = C()
with a, b, c, a, a:
print("Start")
Test harness
from typing import ContextManager
class A(ContextManager):
def __exit__(self, __exc_type, __exc_value, __traceback):
print("A exit")
class B(ContextManager):
def __exit__(self, __exc_type, __exc_value, __traceback):
print("B exit")
class C(ContextManager):
def __exit__(self, __exc_type, __exc_value, __traceback):
print("C exit")
a = A()
b = B()
c = C()
with a, b, c, a, a:
print("Start")
Python 3.7+ output:
Start
A exit
A exit
C exit
B exit
A exit