Beware of defaults
Originally at https://notes.shaunagm.net/post/166818416117/beware-of-defaults
I spent a good portion of yesterday staring at my Django test cases and whimpering. No matter what I tried, no matter how thoroughly I flushed the database between each test case, state seemed to be persisting from test to test. “How is this happening?” I howled to my computer.
It was only when I gave up and went to go celebrate my niece’s second birthday that the answer occurred to me. I’d set a model field default to ‘{}’ instead of ‘dict’.
This is listed as a “common Python gotcha” but I’ve only ever run into it once before in approximately six years of coding in Python. The previous time was in a job interview, which totally stymied me. Luckily it also stymied the interviewer, and I got the job.