ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from functools import lru_cache
@lru_cache(None)
def total(xs):
    return sum(xs)
try:
    print(total([1, 2, 3]))
except TypeError as e:
    print(e)
  1. A. unhashable type: 'list'
  2. B. 6
  3. C. TypeError
  4. D. None

Correct answer: A. unhashable type: 'list'

lru_cache builds a dictionary key from the arguments, so each argument must be hashable. A list is not hashable, so the call fails with TypeError before the function runs. Passing a tuple would work.

More Python Programming questions

Exams that ask this topic