Adding a dictionary to a list using the __iadd__
notation seems to add the keys of the dictionary as elements in the list. Why? For example
a = []
b = {'hello':'world'}
a += b
>> a now stores ['hello']
The documentation for plus-equals on collections doesn't imply to me that this should happen:
For instance, to execute the statement x += y, where x is an instance of a class that has an
__iadd__
() method,x.__iadd__(y)
is called. If x is an instance of a class that does not define a__iadd__()
method,x.__add__(y)
andy.__radd__(x)
are considered, as with the evaluation ofx + y
But, logically, both
a + b # TypeError Exception
and
b + a # TypeError Exception
Are not defined. Furthermore, b+=a
raises a TypeError too. I don't see any special implementation in the source that would explain things, but I'm not 100% sure where to look.
The closest question on SO I found is this one, asking about += on dictionaries, but that's just asking about a data structure with itself. This one had a promising title about list self-addition, but it claims "__add__
" is being applied under the hood, which shouldn't be defined between lists and dictionaries.
My best guess is that the __iadd__
is invoking extend, which is defined here, and then it tries to iterate over the dictionary, which in turn yields its keys. But this seems... weird? And I don't see any intuition of that coming from the docs.