Message354526
This is not a bug, this is the design of function default arguments.
Default arguments in Python use *early binding*, which means they are calculated once, when the function is declared, rather than *late binding*, which means they are calculated each time the function is called.
You can get the effect of late binding by testing for None:
def func(time=None):
if time is None:
time = datetime.datetime.today()
print(time)
Neither choice is right or wrong, they both have advantages and disadvantages. Python chooses early binding for function defaults, and late binding for closures, which have their own, different, gotchas. |
|
Date |
User |
Action |
Args |
2019-10-12 08:49:49 | steven.daprano | set | recipients:
+ steven.daprano, terry.reedy, xtreak, Pouria_ff |
2019-10-12 08:49:49 | steven.daprano | set | messageid: <1570870189.02.0.121820613859.issue38451@roundup.psfhosted.org> |
2019-10-12 08:49:48 | steven.daprano | link | issue38451 messages |
2019-10-12 08:49:48 | steven.daprano | create | |
|