Comment

Comment by capableweb

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.

parent capableweb 3 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton