Here's a strange case I came across.
[1] + {2:3}
# raises an error as expected.
a = [1]
a += {2:3}
# a = [1, 2]
No error is raised in the second case. In fact the list now looks like [1, 2]
. The culprit is the __iadd__
method which when invoked allows us to add a list and a dictionary. I've been looking at the Cpython source code to figure out where this arises but have had no luck so far (I generally don't know what I'm doing when it comes to Cpython source).
Can someone explain this?