Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 858671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:34:23+00:00 2026-05-15T08:34:23+00:00

Suppose you have three objects you acquire via context manager, for instance A lock,

  • 0

Suppose you have three objects you acquire via context manager, for instance A lock, a db connection and an ip socket.
You can acquire them by:

with lock:
   with db_con:
       with socket:
            #do stuff

But is there a way to do it in one block? something like

with lock,db_con,socket:
   #do stuff

Furthermore, is it possible, given an array of unknown length of objects that have context managers, is it possible to somehow do:

a=[lock1, lock2, lock3, db_con1, socket, db_con2]
with a as res:
    #now all objects in array are acquired

If the answer is “no”, is it because the need for such a feature implies bad design, or maybe I should suggest it in a pep? 😛

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:34:24+00:00Added an answer on May 15, 2026 at 8:34 am

    In Python 2.7 and 3.1 and above, you can write:

    with A() as X, B() as Y, C() as Z:
        do_something()
    

    This is normally the best method to use, but if you have an unknown-length list of context managers you’ll need one of the below methods.


    In Python 3.3, you can enter an unknown-length list of context managers by using contextlib.ExitStack:

    with ExitStack() as stack:
        for mgr in ctx_managers:
            stack.enter_context(mgr)
        # ...
    

    This allows you to create the context managers as you are adding them to the ExitStack, which prevents the possible problem with contextlib.nested (mentioned below).

    contextlib2 provides a backport of ExitStack for Python 2.6 and 2.7.


    In Python 2.6 and below, you can use contextlib.nested:

    from contextlib import nested
    
    with nested(A(), B(), C()) as (X, Y, Z):
        do_something()
    

    is equivalent to:

    m1, m2, m3 = A(), B(), C()
    with m1 as X:
        with m2 as Y:
            with m3 as Z:
                do_something()
    

    Note that this isn’t exactly the same as normally using nested with, because A(), B(), and C() will all be called initially, before entering the context managers. This will not work correctly if one of these functions raises an exception.

    contextlib.nested is deprecated in newer Python versions in favor of the above methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 485k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seems that the nodes in a B-Tree can only… May 16, 2026 at 7:44 am
  • Editorial Team
    Editorial Team added an answer You can fairly assume that GCC ignores the register keyword… May 16, 2026 at 7:44 am
  • Editorial Team
    Editorial Team added an answer Like variables with function scope in plain C, blocks literals… May 16, 2026 at 7:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.