list & tuples behavior on +
and +=
operator overloading:
this fails:
>>> [1,2] + (3,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
but this works:
>>> l = [1,2]
>>> l += (3,4)
>>> l
[1, 2, 3, 4]
with set & frozenset things behave differently:
this works:
>>> {1,2} | frozenset({3,4})
{1, 2, 3, 4}
this also works:
>>> s = {1,2}
>>> s |= frozenset({3,4})
>>> s
{1, 2, 3, 4}
Why [1,2] + (3,4)
dont work similarly to {1,2} | frozenset({3,4})
?
Why the two differ? is there a reason for this? is it a backward compatibility thing or something related to internals?
I'm more interested in why its implemented like this not what technically happens under the hood. I suspect that the Python language designed with some careful thinking and there is a reason for this difference which i want to understand.