Skip to content

Instantly share code, notes, and snippets.

@vikjam
Created January 30, 2021 01:05
Show Gist options
  • Save vikjam/b5f4323567c0c4d5b630eb4cb476fbc2 to your computer and use it in GitHub Desktop.
Save vikjam/b5f4323567c0c4d5b630eb4cb476fbc2 to your computer and use it in GitHub Desktop.
Python mutable default arguments
l = ["m"]
def return_list(lst=l):
return(f"lst={lst} and l={l}")
l.append("n")
print(return_list()) # lst=['m', 'n'] and l=['m', 'n']
print(return_list(lst=l)) # lst=['m', 'n'] and l=['m', 'n']
l = ["n"]
print(return_list()) # lst=['m', 'n'] and l=['n']
print(return_list(lst=l)) # lst=['n'] and l=['n']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment