Order of execution for multiple contextmanagers in python

Viewed 23

I couldn't find the answer for this question maybe someone could help me please?

Is the order of execution defined in case of using two contexts like that?

    with open('a.txt', 'w') as f1, open('b.txt', 'w') as f2:
       <some operation>

Am I guaranteed that the first context (here opening 'a.txt') will be executed before second (here opening 'b.txt')?

1 Answers

According to the language reference:

with A() as a, B() as b:
    SUITE

is semantically equivalent to:

with A() as a:
    with B() as b:
        SUITE

So yes, since A() will execute before the body is executed.

Related