Jun-21-2022, 10:53 AM
PEP 671 proposes to introduce syntax to solve the "early binding gotcha" when mutable objects are used as default arguments. Instead of writing
one could instead use the special syntax
The author of the PEP is looking for support for the proposal. Who likes the idea of having a way to set late-bound defaults in the function signature is this way?
For those who are unsure of the difference between early and late binding in this context, it is this:
For anyone who wants to read the background, there are a couple of long discussions on this topic in the Python-Ideas mailing list, starting last year and continuing here with some pauses in between.
1 2 3 |
def func(arg = None ): if arg is None : arg = [] |
arg=>[]
in the function signature.The author of the PEP is looking for support for the proposal. Who likes the idea of having a way to set late-bound defaults in the function signature is this way?
For those who are unsure of the difference between early and late binding in this context, it is this:
- Default values in Python currently are evaluated once, when the function is created, and then re-used when needed. (Early-binding.)
- This PEP proposes to add a second mechanism, which re-evaluates the default expression each time the default value is needed. (Late-binding.)
For anyone who wants to read the background, there are a couple of long discussions on this topic in the Python-Ideas mailing list, starting last year and continuing here with some pauses in between.