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 2 · CPython Internals & Performancefunctools.singledispatch — type-dispatchpredict20 / 161
+100 XP
Task
📝 **Question:** Predict the exact output. \`render\` has registered handlers for \`int\`, \`str\`, \`list\`. Two of the five calls have a twist: (1) \`render(True)\` — \`bool\` has no handler, but is it dispatched to one? (2) \`render(3.14)\` — \`float\` has no handler at all. 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

from functools import singledispatch


@singledispatch
def render(x):
    return "object:default"


@render.register
def _(x: int):
    return f"int:{x}"


@render.register
def _(x: str):
    return f"str:{x}"


@render.register
def _(x: list):
    return f"list[{len(x)}]"


print(render(42))
print(render("hi"))
print(render([1, 2, 3]))
print(render(True))
print(render(3.14))

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…