this post was submitted on 22 Jul 2025
17 points (100.0% liked)

Python

7340 readers
61 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
 

Hi, I recently realised one can use immutable default arguments to avoid a chain of:

def append_to(element, to=None):
    if to is None:
        to = []

at the beginning of each function with default argument for set, list, or dict.

top 8 comments
sorted by: hot top controversial new old
[–] Narann@jlai.lu 4 points 1 week ago (2 children)

This is the way you're supposed to write it in Python.

It is something you get used to, yet I think it's sad.

[–] m_f@discuss.online 5 points 1 week ago (4 children)

You can use mutable default arguments now with a new syntax:

https://peps.python.org/pep-0671/

def bisect_right(a, x, lo=0, hi=>len(a), *, key=None):
def connect(timeout=>default_timeout):
def add_item(item, target=>[]):
def format_time(fmt, time_t=>time.time()):
[–] Doccool@lemmy.world 4 points 1 week ago

Oh wow! This would be great I really hope it's accepted and implemented, makes a lot of sense!

[–] Vulwsztyn@programming.dev 3 points 1 week ago

This is only a Draft for now though

[–] Narann@jlai.lu 2 points 1 week ago (1 children)

Does not seems to work on 3.12:

Python 3.12.11 (main, Jun 29 2025, 16:18:35) [MSC v.1944 64 bit (AMD64)] on win32
>>> def toto(tata=>[]):
  File "<stdin>", line 1
    def toto(tata=>[]):
                  ^
SyntaxError: invalid syntax
[–] logging_strict@programming.dev 1 points 1 week ago* (last edited 1 week ago)

Upvote for the sanity check.

As the OP mentioned, this is a proposed/draft feature that may or may not ever happen.

With these kinda posts, should start a betting pool. To put money down on whether this feature sees the light of day within an agreed upon fixed time frame.

[–] Vulwsztyn@programming.dev 1 points 1 week ago

Yeah, but you can also write it like I proposed which is less boilerplaty and gives the same advantages