contextlib.nested deprecated

T

Thomas Rachel

Hi,

I understand why contextlib.nested is deprecated.

But if I write a program for an old python version w/o the multiple form
of with, I have (nearly) no other choice.

In order to avoid the following construct to fail:

with nested(open("f1"), open("f2")) as (f1, f2):

(f1 wouldn't be closed if opening f2 fails)

I could imagine writing a context manager which moves initialization
into its __enter__:

@contextmanager
def late_init(f, *a, **k):
r = f(*a, **k)
with r as c: yield c

Am I right thinking that

with nested(late_init(open, "f1"), late_init(open, "f2")) as (f1, f2):

will suffice here to make it "clean"?


TIA,

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top