ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
def k(a, b=(1, 2)):
    return b
print(k(1) is k(2))
  1. A. False
  2. B. True
  3. C. TypeError
  4. D. None

Correct answer: B. True

Tuples are immutable, so there is no mutation trap here, but the default value (1, 2) is still built once when the function is defined and the SAME tuple object is returned every time b is omitted, so k(1) is k(2) is True.

More Python Programming questions

Exams that ask this topic