python insanity 2026-07-07 Ok I just discovered this about Python and it's honestly one of the weirdest design decisions I've seen in a programming language. This is a simplified example of what I wrote: def my_cool_func(a, b={}): b[a] = 1 return b My intuition was that assigning `b = {}` in this way would allow me to call `my_cool_func` with one or two arguments, using a new empty dict as a default argument for `b` if none was provided. This is a fairly common pattern used by several programming languages. The crucial difference in Python is, while it does allow for calling the function with one or two arguments, in calls with a single argument, `b` **always has the same value**. It is initialized exactly once when the function is declared, not re-initialized for each call. This is surprising. >>> my_cool_func(1) {1: 1} >>> my_cool_func(2) # uses the same dict value as the first call {1: 1, 2: 1} >>> my_cool_func(3, {}) # different value for b {3: 1} >>> my_cool_func(3) # same default value as the first two calls {1: 1, 2: 1, 3: 1} Hidden global state! Skill issues on my part, maybe. Obviously this would be fine for immutable values like strings, numbers, and None, but for complex values like dicts, lists, sets, etc., this is a huge foot gun. It probably makes something easier to implement in the interpreter to do it this way, but I have a hard time believing this is the behavior anyone actually wants. If it was a tradeoff to simplify the implementation, I think that was a poor choice. Anyway, this is my minor learning for the day. Let me know if you think my take is wrong--I'd love to hear why. -qbasiq