Python Forum
PEP 671 Syntax for late bound function default values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PEP 671 Syntax for late bound function default values
#1
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.
Reply
#2
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?
Reply
#3
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Uniform Function Call Syntax (UFCS) in Python? thegrapevine 1 2,192 Jun-28-2020, 08:17 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020