classification
Title: Datetime definition does not work in function definition as list definition
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Pouria_ff, steven.daprano, terry.reedy, xtreak
Priority: normal Keywords:

Created on 2019-10-11 17:50 by Pouria_ff, last changed 2019-10-12 08:52 by steven.daprano. This issue is now closed.

Messages (6)
msg354478 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-10-11 18:00
Can you please add an example of the issue and the actual/expected behavior?
msg354517 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-10-12 04:46
Without more information, we cannot do anything with this.
msg354519 - (view) Author: Pouria (Pouria_ff) Date: 2019-10-12 05:06
For example
Def a(time=datetime.datetime.today()):
     Print(time)
Output:
    The output of each run is equal to the       first time output
     For exaple :
      If you run
      a()
      Sleep(10)
      a()
      Sleep(10)
       a()
      You see one output three time

در تاریخ شنبه ۱۲ اکتبر ۲۰۱۹،‏ ۸:۱۶ Terry J. Reedy <report@bugs.python.org>
نوشت:

>
> Terry J. Reedy <tjreedy@udel.edu> added the comment:
>
> Without more information, we cannot do anything with this.
>
> ----------
> nosy: +terry.reedy
> status: open -> pending
> versions:  -Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue38451>
> _______________________________________
>
msg354520 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-10-12 05:10
Default arguments are evaluated only once as the function is defined and not per call.

This is a common gotcha : https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument
msg354526 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-10-12 08:49
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.
msg354527 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-10-12 08:52
Oh, I forgot to mention that this is discussed in one of the FAQs:

https://docs.python.org/3/faq/programming.html#why-are-default-values-shared-between-objects
History
Date User Action Args
2019-10-12 08:52:39steven.dapranosetmessages: + msg354527
2019-10-12 08:49:49steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg354526

resolution: not a bug
stage: resolved
2019-10-12 05:10:36xtreaksetmessages: + msg354520
2019-10-12 05:06:50Pouria_ffsetstatus: pending -> open

messages: + msg354519
2019-10-12 04:46:31terry.reedysetstatus: open -> pending
versions: - Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8
nosy: + terry.reedy

messages: + msg354517
2019-10-11 18:00:15xtreaksetnosy: + xtreak
messages: + msg354478
2019-10-11 17:50:10Pouria_ffcreate