ควิซ 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 และ streak

ตัวอย่างคำถาม

ข้อ 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, streak, เหรียญตรา และประวัติ

ใช้ Python เวอร์ชันไหน?

Python 3.12 ทุกคำถามเกี่ยวกับผลลัพธ์ผ่านการรันโค้ดจริงเพื่อตรวจสอบแล้ว

ทำซ้ำได้ไหม?

ได้ ระบบเก็บคะแนนที่ดีที่สุดไว้ และสลับคำถามกับตัวเลือกใหม่ทุกครั้ง