4 ms·
Slightly orthogonal to the original point, but worth noting that operators can also increase confusion when they don't interact with each other in expected ways
by msluyter 7y ago
Slightly orthogonal to the original point, but worth noting that operators can also increase confusion when they don't interact with each other in expected ways. For example, in Python, 'x += y' isn't equivalent to 'x = x + y' in all contexts. Example:
>>> a_tuple = ([1, 2], [3, 4], [5, 6])
>>> a_tuple[2] += ['foo', 'bar']
TypeError: 'tuple' object does not support item assignment
>>> a_tuple
([1, 2], [3, 4], [5, 6, 'foo', 'bar'])
vs
>>> a_tuple = ([1, 2], [3, 4], [5, 6])
>>> a_tuple[2] = a_tuple[2] + ['foo', 'bar']
TypeError: 'tuple' object does not support item assignment
>>> a_tuple
([1, 2], [3, 4], [5, 6])
Edited to add, to understand what's happening here, check out the documentation on in place operators: https://docs.python.org/3/library/operator.html#in-place-operators https://docs.python.org/3/library/operator.html#in-place-ope...