contextlib.nested deprecated

Discussion in 'Python' started by Thomas Rachel, Mar 23, 2012.

  1. 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
     
    Thomas Rachel, Mar 23, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barney Barumba
    Replies:
    0
    Views:
    565
    Barney Barumba
    Jul 23, 2003
  2. Paul Rubin

    contextlib.closing annoyance

    Paul Rubin, Jun 23, 2007, in forum: Python
    Replies:
    1
    Views:
    311
    Klaas
    Jun 25, 2007
  3. brasse

    contextlib.nested()

    brasse, Nov 6, 2008, in forum: Python
    Replies:
    7
    Views:
    506
    brasse
    Nov 7, 2008
  4. Neal Becker

    @contextlib question

    Neal Becker, Nov 7, 2008, in forum: Python
    Replies:
    1
    Views:
    232
    Peter Otten
    Nov 7, 2008
  5. Replies:
    9
    Views:
    317
    Steven D'Aprano
    Feb 1, 2012
Loading...

Share This Page