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

Python

7345 readers
12 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.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] Narann@jlai.lu 4 points 1 week ago (7 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.

[โ€“] 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

load more comments (6 replies)