- Executores
-
Aprender
Aprender Python Exemplos executáveis, NumPy, pandas e mais Aprender IA / Machine LearningRedes neurais, ML e LLMs do zero Aprender C e C++ Ponteiros, memória, STL e C++ moderno Aprender C#Fundamentos, POO, LINQ, async e exercícios .NET Aprender MatplotlibGráficos de linha, barras, dispersão e pizza com visualizadores Aprender RustOwnership, traits, tratamento de erros e threads Aprender GoSlices, interfaces, goroutines e channels Algoritmos em Python Busca, ordenação, recursão e visualizações O(n) Algoritmos em C e C++ Compare implementações em C e C++ Programação para crianças Aprenda a programar brincando!
- QuizzesNovo
- DesafiosNovo
- Para professoresNovo
- Atualizações
- Me pague um café
Quiz de Python: Context Managers & with
This Python quiz explains the with statement from the inside. You will trace __enter__ and __exit__, see how exceptions are passed to __exit__ and when they are swallowed, and build context managers with contextlib.
Pergunta de exemplo
Pergunta 1 de 10What does this print?
class Tag:
def __enter__(self):
print("enter", end=" ")
return "body"
def __exit__(self, *exc):
print("exit", end="")
with Tag() as t:
print(t, end=" ")
Mostrar a resposta
B. enter body exit with calls __enter__ first and binds its return value, "body", to t. Then the block runs, and __exit__ runs last.
O que este quiz cobre
- What
withcalls and whatasreceives __exit__and exceptions, including suppressing them@contextmanagerwithyieldandtry/finally- Several managers in one
withand their exit order contextlib.suppress
Perguntas frequentes
Preciso de uma conta?
Não. Qualquer pessoa pode jogar. Entre para guardar seu XP, sequência, badges e histórico.
Qual versão do Python?
Python 3.12. Toda pergunta sobre a saída foi conferida rodando o código.
Posso refazer?
Pode. Sua melhor nota fica salva, e as perguntas e respostas são embaralhadas a cada vez.