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:
def my_busted_record_keeper(last_update=datetime.date.today()): ... print("last update was %s" % last_update)
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:
def my_busted_record_keeper(last_update=None): if last_update is None: last_update = datetime.date.today() ... print("last update was %s" % last_update)
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.
Comments !