Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Context Manager Stacking | Advanced File Handling & Context Managers
Python Structural Programming
Seção 2. Capítulo 7
single

single

Challenge: Context Manager Stacking

Deslize para mostrar o menu

You have explored how to create your own context managers and how to use them in nested and multiple arrangements. Now you will need to design and implement custom context managers that work both alone and together, ensuring that resources are acquired and released correctly. Pay attention to how exceptions are handled and how the order of entering and exiting context managers affects program behavior.

Tarefa

Deslize para começar a programar

Create two simple context manager classes called ManagerA and ManagerB. Each one should print a message when you enter and exit their context:

  • When entering, print "Entering ManagerA" or "Entering ManagerB".
  • When exiting, print "Exiting ManagerA" or "Exiting ManagerB".

Then, do the following:

  • Use ManagerA and ManagerB in nested with statements (one inside the other).
  • Use both managers in a single with statement (on the same line).

Check the printed messages to see the order in which each manager is entered and exited.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 7
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt