- Esecutori
-
Impara
Impara Python Esempi eseguibili, NumPy, pandas e altro Impara l'IA / Machine LearningReti neurali, ML e LLM da zero Impara C e C++ Puntatori, memoria, STL e C++ moderno Impara C#Fondamenti, OOP, LINQ, async ed esercizi .NET Impara MatplotlibGrafici a linee, barre, dispersione e torta con visualizzatori Impara RustOwnership, trait, gestione degli errori e thread Impara GoSlice, interfacce, goroutine e canali Algoritmi Python Ricerca, ordinamento, ricorsione e visualizzazioni O(n) Algoritmi C e C++ Confronta le implementazioni in C e C++ Programmazione per bambini Impara a programmare giocando!
- QuizNuovo
- SfideNuovo
- Per docentiNuovo
- Aggiornamenti
- Offrimi un caffè
Quiz 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.
Domanda di esempio
Domanda 1 di 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=" ")
Mostra la risposta
B. enter body exit with calls __enter__ first and binds its return value, "body", to t. Then the block runs, and __exit__ runs last.
Cosa copre questo quiz
- What
withcalls and whatasreceives __exit__and exceptions, including suppressing them@contextmanagerwithyieldandtry/finally- Several managers in one
withand their exit order contextlib.suppress
Domande frequenti
Serve un account?
No. Chiunque può giocare. Accedi per salvare XP, serie, badge e cronologia.
Quale versione di Python?
Python 3.12. Ogni domanda sull'output è stata verificata eseguendo il codice.
Posso rifarlo?
Sì. Il tuo miglior punteggio resta salvato, e domande e risposte vengono mescolate ogni volta.