r/programminghorror • • Oct 26 '26

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

Show parent comment

2

u/trutheality Oct 26 '26

The behavior isn't even that weird: if you were to create some mutable global and set it as a default parameter in a function you wouldn't be surprised that it's accumulating changes from different calls to the function.

The key part is recognizing that the LHS in a default parameter assignment runs when the function is defined and not when it is called.

6

u/nakurtag Oct 26 '26

Name one language with the same behavior

1

u/Double_Cost4865 Oct 26 '26

I mean, C/C++? You can pass pointers/references and they will modify in place