This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients Pouria_ff, steven.daprano, terry.reedy, xtreak
Date 2019-10-12.08:49:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1570870189.02.0.121820613859.issue38451@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2019-10-12 08:49:49steven.dapranosetrecipients: + steven.daprano, terry.reedy, xtreak, Pouria_ff
2019-10-12 08:49:49steven.dapranosetmessageid: <1570870189.02.0.121820613859.issue38451@roundup.psfhosted.org>
2019-10-12 08:49:48steven.dapranolinkissue38451 messages
2019-10-12 08:49:48steven.dapranocreate