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 7 · Memory, GC, modern typing@runtime_checkable Protocol — structural typing + its blind spotspredict123 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. The protocol Sized requires __len__. Real types with __len__ pass; types without it fail. The last case — `FakeSized` — has a non-callable attribute called `__len__`, and runtime_checkable still says True because it only checks existence, not call-ability. This is the senior gotcha worth knowing. 📋 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 Sized(Protocol):
    def __len__(self) -> int: ...


class Bag:
    def __init__(self, items: list) -> None:
        self._items = items

    def __len__(self) -> int:
        return len(self._items)


class Empty:
    pass


class FakeSized:
    __len__ = "not actually a method"


print(isinstance(Bag([1, 2, 3]), Sized))
print(isinstance(Empty(), Sized))
print(isinstance([1, 2], Sized))
print(isinstance("hello", Sized))
print(isinstance(42, Sized))
print(isinstance(FakeSized(), Sized))

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…