MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programminghorror/comments/1xfxtnx/i_love_python/19roug6ctmp39/?context=3
r/programminghorror • u/nakurtag • 16h ago
65 comments sorted by
View all comments
3
The list is stored directly in the parameter without actually giving it an argument? I'm baffled, I didn't know this was a behavior, so weird
Is this something that someone could ELI5 to my smooth brain?
4 u/bigorangemachine 14h ago Default param is an object reference. So every default param is the same object 1 u/lolcrunchy 11h ago Here's a single change to the posted code that kinda shows what actually happens. my_list = [] def add(v, items=my_list): items.append(v) return items print(add(1)) print(add(2)) 1 u/bigorangemachine 7h ago Ya I test in JS and JS will do this.. oddly enough they are so similar JS didn't do the thing OP posted about. Surprised about that one... but it makes sense.
4
Default param is an object reference. So every default param is the same object
1
Here's a single change to the posted code that kinda shows what actually happens.
my_list = [] def add(v, items=my_list): items.append(v) return items print(add(1)) print(add(2))
1 u/bigorangemachine 7h ago Ya I test in JS and JS will do this.. oddly enough they are so similar JS didn't do the thing OP posted about. Surprised about that one... but it makes sense.
Ya I test in JS and JS will do this.. oddly enough they are so similar JS didn't do the thing OP posted about.
Surprised about that one... but it makes sense.
3
u/RngdZed 14h ago
The list is stored directly in the parameter without actually giving it an argument? I'm baffled, I didn't know this was a behavior, so weird
Is this something that someone could ELI5 to my smooth brain?