Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 6 · FAANG-2026 deep divestyping.Protocol — runtime_checkable's hidden holepredict109 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. Four classes are tested against a runtime-checkable \`Closeable\` Protocol (one method: \`close()\`). \`File\` is the canonical case. \`Bag\` has no \`close\`. \`Stream\` defines \`close\` without inheriting. \`Weird\` has \`close = 42\` — an int. What does isinstance say for each? 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

from typing import Protocol, runtime_checkable


@runtime_checkable
class Closeable(Protocol):
    def close(self) -> None: ...


class File:
    def close(self):
        return "file"


class Bag:
    pass


class Stream:
    """Doesn't inherit Closeable; still passes by structural matching."""
    def close(self):
        return "stream"


class Weird:
    """Has a 'close' attribute, but it's an int."""
    close = 42


print(isinstance(File(), Closeable))
print(isinstance(Bag(), Closeable))
print(isinstance(Stream(), Closeable))
print(isinstance(Weird(), Closeable))

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…