Good question. Doesn't seem invincibly immutable to me. Python 2.7.2:
>>> from decimal import Decimal
>>> d = Decimal('1.23')
>>> d
Decimal('1.23')
>>> f = d
>>> f
Decimal('1.23')
>>> d._exp = 1
>>> f
Decimal('1.23E+3')
I do see that the documentation says it's immutable. I guess they mean if you use the documented interface, it's immutable. E.g.
>>> d += 100
>>> f
Decimal('1.23E+3')
>>> d
Decimal('1330')
The way this works is simply that Decimal does not define operator overloads for ?= operators: functions like __i[op]__
. In that case, d += 100
translates to d = d.__add__(100)
, which returns a new object and changes the identity of d while not affecting the original object.