Steven D'Aprano <steve+pyt...@pearwood.info> added the comment:

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

----------
nosy: +steven.daprano

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39556>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to