Immutability of Floats, Ints and Strings in Python

Random832 random832 at fastmail.com
Mon Nov 28 11:01:54 EST 2016


On Fri, Nov 25, 2016, at 06:33, Ned Batchelder wrote:
> A Python implementation can choose when to reuse immutable objects and
> when not to.  Reusing a value has a cost, because the values have to
> be kept, and then found again. So the cost is only paid when there's
> a reasonable chance that the values will actually be needed again.
> And that cost has to be weighed against the opposite cost of simply
> making a new object instead.

Of course, there are more complicated costs to consider. For an
implementation where objects do not have a naturally persistent object
identity (such as an immovable address in memory as in cpython) they may
consider it easier to have an "object identity" that consists of the
whole value for immutable types rather than pay whatever costs are
associated with having a unique and unchanging "id" value. It's also not
hard to imagine an implementation that has "references" consisting of a
tagged union and incorporating the value in the "reference" itself (and
therefore the id) for floats and small integer/string values, though I
can't name any implementation (of Python, anyway - it's not uncommon for
Lisp) that does so.



More information about the Python-list mailing list