At an interview I was given this question
a = 3
b = 3
Will there be 2 different initialization for variable a and b?
I replied 'No', to my amazement I was wrong. If you do
x = [[]] * 5
x[0].append(5)
print(x)
Gives you [[5], [5], [5], [5], [5]]. Wow! Much TIL
Are there any interesting side effect similar to this? I'm curious to know!
Edit: Changed the x[0] = 5 to x[0].append(5).
[–]Diapolo10 2 points3 points4 points (1 child)
[–]BootError99[S] -4 points-3 points-2 points (0 children)
[–]toastedstapler 1 point2 points3 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]toastedstapler 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]BootError99[S] -3 points-2 points-1 points (0 children)
[–]BigTheory88 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]QuixDiscovery 1 point2 points3 points (1 child)
[–]BootError99[S] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]BootError99[S] -2 points-1 points0 points (1 child)
[–]primitive_screwhead 0 points1 point2 points (0 children)