Combine two context managers into one
问题 I use Python 2.7 and I know that I can write this: with A() as a, B() as b: do_something() I want to provide a convenience helper which does both. The usage of this helper should look like this: with AB() as ab: do_something() Now AB() should do both: Create context A() and create context B(). I have no clue how to write this convenience helper 回答1: Don't re-invent the wheel; this is not as simple as it looks. Context managers are treated as a stack , and should be exited in reverse order in