I understand why contextlib.nested is deprecated .
But if I write a program for an old version of python without a plural form with (ie, <2.7), I (almost) have no other choice.
To avoid a failure of the following design:
with nested(open("f1"), open("f2")) as (f1, f2):
( f1 will not be closed if opening f2 fails because the context manager is not entered)
I could imagine a context manager that translates initialization into my __enter__ :
@contextmanager def late_init(f, *a, **k): r = f(*a, **k) with r as c: yield c
Do I think that right
with nested(late_init(open, "f1"), late_init(open, "f2")) as (f1, f2):
will be enough to make it "clean"?
The given usage example is just an example. Imagine that you have a list of files whose length was not prematurely known. Then neither 2.7 linked with can be used, nor nested up to 2.7 words with several indentation with .
I probably should be more detailed.
This question solves the problem at a glance: the function call is executed in a safe place, so that a failure can be detected and handled accordingly.
My question is: does it cure a flaw or am I having other problems?