Python Forum
PEP 671 Syntax for late bound function default values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: PEP 671 Syntax for late bound function default values (/thread-37515.html)



PEP 671 Syntax for late bound function default values - stevendaprano - Jun-21-2022

PEP 671 proposes to introduce syntax to solve the "early binding gotcha" when mutable objects are used as default arguments. Instead of writing

def func(arg=None):
    if arg is None:
        arg = []
one could instead use the special syntax 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.


RE: PEP 671 Syntax for late bound function default values - Gribouillis - Jun-21-2022

It is a good idea. It is frequent to have the structure
if arg is sentinel:
    arg = some_value
The pep would make better and cleaner code. +1

Guido van Rossum Wrote:*Pronouns: he/him **(why is my pronoun here?)*
This is so ridiculous. Why does GvR add this to his signature in discussions? Or perhaps it is politically correct must for people working for Microsoft?


RE: PEP 671 Syntax for late bound function default values - DeaD_EyE - Jun-21-2022

I thought it's already in Python 3.11, but it's still a draft.

This change can help to clean up the existing code bases. I like the idea because it's directly visible in the function signature, and you don't have to look in the code of the function. The old way to archive this, is a bit verbose.


(Jun-21-2022, 12:32 PM)Gribouillis Wrote: This is so ridiculous. Why does GvR add this to his signature in discussions? Or perhaps it is politically correct must for people working for Microsoft?
He is a part of BigTech, which is a part of Transhumanism (Eugenics).
Google: Yuval Noah Harari