Author steven.daprano
Recipients Kevin Young, ammar2, steven.daprano
Date 2020-02-05.20:41:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Ammar: it is definitely a feature that default values are created only once, when the function is created, not over and over again every time the function is called. (These are sometimes called "early binding" and "late binding" respectively.)

With early binding, you know what the default value is: it is the object at the time the function was created.

(But note that *mutable* objects may be mutated from one call to the next: they are not "reset" to their initial value each time. This is occasionally useful, to implement static storage across function calls, but also often a Gotcha that trips people up.)

With late binding, a default value like this:

    def function(value=a+b+c):

could change from one call to the next, if the values a, b or c change.

Both early and late binding have advantages and disadvantages, but if a language only has one, it is better to have early binding (as Python has) and let the coder do the late binding inside the function:

    def function(value=None):
        if value is None:
            value = a+b+c
Date User Action Args
2020-02-05 20:41:36steven.dapranosetrecipients: + steven.daprano, ammar2, Kevin Young
2020-02-05 20:41:36steven.dapranosetmessageid: <>
2020-02-05 20:41:36steven.dapranolinkissue39556 messages
2020-02-05 20:41:35steven.dapranocreate