4 ms·
I gave it a try as well, and GPT-4 taught me something new about Python (I don't normally write Python, so it's not surprising): def add_to_list(value, tar
by capableweb 4y ago
I gave it a try as well, and GPT-4 taught me something new about Python (I don't normally write Python, so it's not surprising):
def add_to_list(value, target_list=[]):
target_list.append(value)
return target_list
list1 = add_to_list(1)
list2 = add_to_list(2)
print(list1) # [1, 2]
print(list2) # [1, 2]
In this example, one might expect list1 to contain only 1 and list2 to contain only 2. However, because the default value for the target_list parameter is mutable, it gets shared between function calls, leading to unexpected behavior.
The other examples it gave me were not that unexpected. Prompt was:
> Show me examples of where Python type inference works in unexpected ways.
- roywiggins 4y ago> because the default value for the target_list parameter is mutable, it gets shared between function calls bit of a weird way to phrase it, I think they're always shared between function calls, you just don't notice when they're not mutable.
- masklinn 4y agoThat is correct, default values are initialised at compilation time and stored in the function object.
- afraca 4y agoSome people might not know this yet, the common idiom is to have the default value be None, and then start your function with checking if it's None and initializing to empty list.
- reportgunner 4y agoThe whole function just makes no sense, it's just `target_list.append(value)` with extra steps.
- capableweb 4y agoYou're missing the point if you're looking at how "useful" this example function is
- reportgunner 4y agoNo, the example is just not good.
- capableweb 4y agoAh, yeah, compared to the examples in the video that makes so much sense in a normal day to day application. I commonly do the following operations without much thought: [] + [] {} + {} [] + {} {} + [] Great examples :) The point is not what the data or naming of the functions/variables is, but what's happening when you run it...
- reportgunner 4y ago[] + [] is more likely to happen than taking list.append(value) and defining a function that does list.append(value) and then returns whatever list.append(value) returns. Hint: list.append(value) returns None
- xdennis 4y ago`[] + []` is unlikely to happen except by accident if you don't know that's how array concatenation is done in JavaScript. But wanting to provide default values of [] is quite common in Python. Seeing this workaround is quite common in Python: def func(arg=None): if arg is None: arg = [] ...
- reportgunner 4y agoI'm talking about the following where we don't intend to do [] + [] but we feed some lists to some functions that do something to the lists and we end up with empty lists in some cases without inspecting the contents of a and b after we feed it to the functions. Why would you define a default argument of an empty list if you're not gonna check if it's a list and if it's empty? a = [1,2] someFunction(a) b = [3,4] someOtherFunction(b) a + b
- xdennis 4y agoThe Wat part is this: print(list1) # [1, 2] print(list2) # [1, 2] Some would expect it to be: print(list1) # [1] print(list2) # [2] The reason it's weird is because Python evaluates default arguments at function definition time rather that function call. This means that it's unsafe to use mutable values as default values in params. See the equivalent in JavaScript: function addToList(value, targetList=[]) { targetList.push(value); return targetList; } const list1 = addToList(1); const list2 = addToList(2); console.log(list1); // [1] console.log(list2); // [2]