Steven D'Aprano writes:

 > "Write `arg=default` if you want the default to be evaluated just once, 
 > and `arg=late default` if you want the default to be evaluated each time 
 > it is needed. If you are not sure which one you need, use the
 > first."

Which of course is ambiguous, since the argument may be referenced
many times in the function body or only late in the body.  Someone who
doesn't yet understand how early binding of mutable defaults works is
also somewhat likely to misunderstand "when needed" as a promise that
resolves to an object the first time it is referenced, or as a thunk
that gets run every time it is referenced, both of which are
incorrect.  What you should write to be (more) accurate is

    Write `arg=default` if you want the default to be evaluted when
    the function is defined [and the value to be stored in the
    function to be used when it is called], and `arg=late default` if
    you want the default to be evaluated each time the function is
    called.  If you are not sure which one you need, ask someone to
    help you, because using the wrong one is a common source of bugs.

The part in brackets is a gloss that might be helpful to the beginner,
who might experience a WTF at "evaluated when defined"

To be honest, I was surprised you chose early binding for "when in
doubt".  I would expect that "early binding when late is appropriate"
is a much more common bug for beginners than "late binding when early
is appropriate".  Of course I may be biased because it's the only bug
now, but I would think that would continue to be true for beginners if
late binding is made available.  Especially when they're testing code
in the interactive interpreter.  There are probably many programs
where the function is called with the argument missing only once, in
which case it doesn't matter, until you invoke the function repeatedly
in the same context.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PYTLC4NCU2XZJMVMGRULTLPJPRDRMAAL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to