FAQ: Context Managers - Nested Context Managers

This community-built FAQ covers the " Nested Context Managers" exercise from the lesson “Context Managers”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Python 3

FAQs on the exercise _ Nested Context Managers_

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello! I have a question about nested context managers. This lesson says:

with open('teacher.txt', 'w') as teacher, open('student.txt', 'r') as student:
 teacher.write(student.read())

Notice:

  • The with statement is being called once but invoking two context managers. This is a single-line nested with statement .

But the quiz (https://www.codecademy.com/courses/learn-intermediate-python-3/quizzes/int-python-context-managers-quiz) says that the valid way of nesting these context managers should be written like this :

with ContextManagerA('fileA.txt', 'a') as A, with ContextManagerB('fileB.txt', 'a') as B:

Correct! Context managers can be nested in one line using a comma and separate with statements

I’ve tested on this lessons exercise and got a syntax error with two ‘with’s’ separated by comma; no error with a single ‘with’. Would it be right to consider that lesson got right explanation, and the quiz has a mistake?

#causes SyntaxError: invalid syntax:
with poem_files('poem.txt', 'r') as poem,  with card_files('card.txt', 'w') as card:
    print(poem)
    print(card)
    card.write(poem.read())

#no errors
with poem_files('poem.txt', 'r') as poem,  card_files('card.txt', 'w') as card:
    print(poem)
    print(card)
    card.write(poem.read())
1 Like

Looks like a mistake here. The question seems somewhat torn between choices. Whilst you can nest context mangers like-

with CntMgrA as a:
    with CntMgrB as b:
        dosomestuff...

… this is often considered rather ugly due to the pointless excess of indentation and the standard syntax is then to use-

with CntMgrA as a, CntMgrB as b:

… which you and the lesson recognise as the correct choice for that simpler syntax.

For the actual language rules- https://docs.python.org/3/reference/compound_stmts.html#the-with-statement

I’m not sure where the lesson was going for (the basic nesting or the comma separated arguments) but the syntax is most definitely wrong for that “correct” answer at the moment. I’ll raise it as a bug for sure.

1 Like

sorry for belated answer and thank you for the explanations! :grinning:

1 Like