default function parameter values in python
Sure, we all know that“default parameter values are evaluated when the function definition is executed” in python. But what may not be clear is a fun way to introduce time-dependent bugs. See, this:
|
|
Is totally broken.
If the code runs for more than a day, we aren’t going to get the results we expect since last_update will have been evaluated at definition time which is now yesterday. Instead we should do something like:
|
|
Usually this isn’t a problem because my code crashes at least once a day, but I know I’ve made this mistake before and there’s a non-zero chance something like this running in production. Quite embarrassing.