You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't really work with nested lists but I know that it's quite common among the sci community (in fact I'm opening this issue after seeing this tweet).
I dug into the tracker to see if it was suggested before, but I could not find anything. I forgot to record what I tried, though.
Anyway, here is an excerpt of the issue:
# It might not be intuitive that the lists inside are actually the same# It is very likely not to be intended, and a footgun all the timefoo: list[list[int]] = [[]] *4foo[0].append(42)
print(foo) # [[42], [42], [42], [42]]
It's quite silly and experienced Python programmers usually don't fall for it, but people for whom coding is not their main activity and/or beginners can and do.
This rule could also be extended to passing a list literal as the second argument of dict.fromkeys, although this might be considered out of this issue's scope.
(+ I don't know by heart all the places where this "lists that actually share the same memory" footgun appears, but feel free to list some more.)
The text was updated successfully, but these errors were encountered:
Hello.
I don't really work with nested lists but I know that it's quite common among the sci community (in fact I'm opening this issue after seeing this tweet).
I dug into the tracker to see if it was suggested before, but I could not find anything. I forgot to record what I tried, though.
Anyway, here is an excerpt of the issue:
It's quite silly and experienced Python programmers usually don't fall for it, but people for whom coding is not their main activity and/or beginners can and do.
This rule could also be extended to passing a list literal as the second argument of
dict.fromkeys
, although this might be considered out of this issue's scope.(+ I don't know by heart all the places where this "lists that actually share the same memory" footgun appears, but feel free to list some more.)
The text was updated successfully, but these errors were encountered: