Квиз по 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.

Средний 10 вопроса ~6 минуты Проходной балл: 80% Python 3.12

Выберите режим

Вы играете как гость: войдите, чтобы сохранить XP и серию.

Пример вопроса

Вопрос 1 из 10

What 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=" ")
Показать ответ

B. enter body exit with calls __enter__ first and binds its return value, "body", to t. Then the block runs, and __exit__ runs last.

Что проверяет этот квиз

  • What with calls and what as receives
  • __exit__ and exceptions, including suppressing them
  • @contextmanager with yield and try/finally
  • Several managers in one with and their exit order
  • contextlib.suppress

Частые вопросы

Нужен ли аккаунт?

Нет. Играть может любой. Войдите, чтобы сохранять XP, серию, значки и историю.

Какая версия Python?

Python 3.12. Каждый вопрос о выводе программы проверен запуском кода.

Можно пройти ещё раз?

Да. Лучший результат сохраняется, а вопросы и ответы каждый раз перемешиваются.