Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Default Argument Handling in immutable_default_args.py #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/mutable_default_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ def append_element(elem, L=[]):

# Correct way 🔥: Use None ✅
def better_append(elem, L=None):
if L is None:
if L:
L = []
L.append(elem)
return L

L1 = better_append(21) # [21]
L2 = better_append(42) # [42]
L2 = better_append(42) # [42]

# Correct way 🔥: Use None ✅
def better_append(elem, L=None):
# If L is None or evaluates to False (e.g., an empty list), initialize it to an empty list.
L = L or []

# Append the provided element to the list.
L.append(elem)

# Return the modified list.
return L