Skip to content

Instantly share code, notes, and snippets.

@abhishekmishragithub
Last active July 2, 2020 09:43
Show Gist options
  • Save abhishekmishragithub/bf1a95e551fca95efbbe034ef664eb7d to your computer and use it in GitHub Desktop.
Save abhishekmishragithub/bf1a95e551fca95efbbe034ef664eb7d to your computer and use it in GitHub Desktop.
Python Gotcha : Mutable Default Arguments ( Python’s default arguments are evaluated once when the function is defined, not each time the function is called. This means that if you use a mutable default argument and mutate it, you will and have mutated that object for all future calls to the function as well.)
def append_to(element, to=[]):
to.append(element)
return to
my_list = append_to(12)
print(my_list)
my_other_list = append_to(42)
print(my_other_list)
# expected output : [12] [24]
# actual output : [12] [12, 42]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment