Last active
July 2, 2020 09:43
-
-
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.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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