ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
import functools
@functools.singledispatch
def show(x): return "obj"
@show.register
def _(x: int): return "int"
@show.register
def _(x: list): return "list"
print(show(1), show(True), show([1]), show(1.5), show((1,)))
  1. A. int int list obj obj
  2. B. int obj list obj list
  3. C. int int list int obj
  4. D. int bool list obj obj

Correct answer: A. int int list obj obj

singledispatch dispatches on the type of the first argument using the MRO, so bool (a subclass of int) uses the int version. float and tuple have no registration and fall back to the base function.

More Python Programming questions

Exams that ask this topic