What is the output of the following? “`python def foo(a, b=[]): b.append(a) return b print(foo(1)) print(foo(2)) “`

Python Developer Hard

Python Developer — Hard

What is the output of the following? “`python def foo(a, b=[]): b.append(a) return b print(foo(1)) print(foo(2)) “`

Key points

  • The default value of a mutable object in a function parameter is shared across all calls.
  • When `b` is modified in one call, it affects subsequent calls.
  • This behavior can lead to unexpected results if not handled carefully.

Ready to go further?

Related questions