Lawful != Mutable (was Can Python function return multiple data?)

Chris Angelico rosuav at gmail.com
Sat Jun 20 22:32:23 EDT 2015


On Sun, Jun 21, 2015 at 11:59 AM, Rustom Mody <rustompmody at gmail.com> wrote:
> Recent thread on python ideas
> https://mail.python.org/pipermail/python-ideas/2015-June/034177.html
>
> Since "python's immutable" ≠ "really immutable", we now need a "really immutable"

That's because it requires mutable memory to keep track of reference
counts. In a non-refcountinig Python, strings and integers can be
truly immutable; that thread is suggesting (among other options)
simply storing the refcounts in a separate table. Conceptually, a
string or integer IS immutable, but CPython currently needs to be able
to fiddle with some bookkeeping data about them, in order to pretend
it has plenty of memory. A Python implementation could theoretically
just abandon its strings whenever it's done with them, wasting gobs of
memory in the process, and having them in read-only memory. More
practically, a pure mark-and-sweep GC (such as MicroPython uses)
trades prompt disposal for simple management, and in doing so,
achieves (I think) the same total immutability.

You have to distinguish between Python (the language) and CPython (the
implementation) here. Python's notion of mutability has nothing to do
with thread safety. You could achieve perfect thread safety for
immutables by simply replicating them into every thread; it'd be
inefficient, but perfectly legal.

ChrisA



More information about the Python-list mailing list