Post

D. [1, 2]
The classic Python trap. Default list b=[] is created ONCE when the function is defined, not on each call.
So func(1) appends 1 → [1]
Then func(2) appends to the SAME list → [1, 2]
This is why you always use b=None and then b = b or [] inside the function. Every Python dev learns this the hard way exactly once 😂
English

@PythonPr The correct answer is [1] [1, 2]
Why does this happen?
The trick lies in how Python handles default arguments. Here is the breakdown:
Persistent Objects: Default arguments are evaluated only once at the time the function is defined, not every time the function is called.
English

Answer: D) [1, 2]
Because the default argument is mutable and shared across function calls.
The list is created when the function is defined, not when it is called.
Each call appends to the same list, so values accumulate over time.
This is why mutable default arguments should be avoided in Python.
English

@PythonPr Option D : [1, 2]
Python’s default arguments are evaluated once, not each call. The list b is shared across calls. First call appends 1. Second call reuses the same list and appends 2, so it becomes [1, 2]. Sneaky, but totally Pythonic! 🐍🍓
English

D. [1, 2]
This is because default arguments in Python are only created once — when the function is defined, not each time it's called.
So the list b=[] is shared across all calls to the function.
Here's what happens:
func(1) → appends 1 to b, so b becomes [1]
func(2) → reuses the same list, appends 2 → b is now [1, 2]
English

@PythonPr D) python trap i almost fell for that
English

@PythonPr C: [1] [2]
as the function has a default parameter b as empty list (i.e. []),
and func is being called with a single param (i.e. a), being passed as
1 in first call, and
2 in second call,
it would be added to the empty list and returned.
English











