The behavior of mutable default arguments in Python is among the most evil things about language.
I know that beginners have a lot of different tools to learn, and learning how to use an IDE if you have never used a programming editor is an additional burden, but I still think that people should learn how to use some linter or other relatively early.
8
u/ParticularLook5927 Apr 09 '26
Answer: B) [1], [1, 1]
This happens because default mutable arguments (like lists) are shared across function calls.
The list x=[] is created only once when the function is defined, not every time the function is called.
So: First call → [1] Second call → same list gets another 1 → [1, 1]
To fix this, use:
def foo(x=None):
This way, a new list is created every time.
2
u/jpgoldberg Apr 09 '26 edited Apr 11 '26
The behavior of mutable default arguments in Python is among the most evil things about language.
I know that beginners have a lot of different tools to learn, and learning how to use an IDE if you have never used a programming editor is an additional burden, but I still think that people should learn how to use some linter or other relatively early.
2
u/Leviterion_HU Apr 10 '26
A) [1, 1] bacause of mutable default arguments
1
u/ShadowKing1231 Apr 11 '26
Number B
1
u/Big_Parsnip_9053 Apr 12 '26
poop