ProgrammingHardAccenture pattern

What is printed?

What is printed?
def f2(x, y, *, z=None):
    if z is None:
        z = []
    z.append(x + y)
    return z
print(f2(1, 2))
print(f2(3, 4))
  1. A. [3] [3, 7]
  2. B. [3] [7]
  3. C. [3, 7] [3, 7]
  4. D. [7] [3]

Correct answer: B. [3] [7]

Using the None-sentinel pattern, z gets a FRESH empty list on every call that omits it, avoiding the mutable-default trap; each call therefore returns an independent one-element list.

More Python Programming questions

Exams that ask this topic