Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59099

'isimmutable' and 'ImmutableNester'

Date 2013-11-11 21:47 +0100
Subject 'isimmutable' and 'ImmutableNester'
From Frank-Rene Schäfer <fschaef@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2397.1384202874.18130.python-list@python.org> (permalink)

Show all headers | View raw


I prepared a PEP and was wondering what your thoughts are about it:

  PEP:            <pep number>
  Title:          ``isimmutable(Obj)`` and/or ``ImmutableNester``
  Version:        <version string>
  Last-Modified:  <date string>
  Author:         Frank-Rene Schaefer, fschaef@users.sourceforge.net
* BDFL-Delegate:  <PEP czar's real name>
* Discussions-To: fschaef@users.sourceforge.net
  Status:         Draft
  Type:           <Standards Track | Informational | Process>
* Content-Type:   text/x-rst
* Requires:       <pep numbers>
  Created:        11-nov-2013
* Python-Version: 2.7.1
  Post-History:   <dates of postings to python-list and python-dev>
* Replaces:       <pep number>
* Superseded-By:  <pep number>
* Resolution:     <url>

General Idea
============

A built-in function 'isimmutable()' shall tell efficiently whether the object
of concern is mutable or not. That is it must reflect on the whole object tree
whether it contains mutable elements or not.  For example, in the code fragment

::
    verdict_0 = isimmutable(3.14)
    verdict_1 = isimmutable((1,2,3))
    verdict_2 = isimmutable(((1,2),(2,3),(3,4)))

all verdicts are 'True' because the tested objects consist of purely immutable
components. However, the ``x`` in

::
    x       = (1,(2,"abc", [1,2,3]))
    verdict = isimmutable(x)

triggers the verdict to be 'False' because ``x[1][2]`` is a list and therefore
mutable.

It may be conceivable to have a special class-type called ``ImmutableNester``
which has no write-access member functions and does not allow its derived
classes to have write-access member functions. Instead, any derived class
aggregates members at the time of construction. At this point in time, when
members are nested in the class, it is checked if the members are of subclasses
of ``ImmutableNester``.

The advantage of the ``isimmutable()`` function is that no extra class
functionality needs to be implemented. The disadvantage is that the
immutability must be checked manually and at each time the object is used. The
``ImmutableNester`` class type checks for immutability once, at construction
time and no further manual checks are necessary.

Rationale
=========

If an object is immutable then copying of it can be safely be replaced by a
setting of a reference. The principal scenario is when an instance A gives an
instance B access to some data D under the provision that B does not change it.
Therefore, B must either clone the data or it must be safe to assume that the
data cannot change, i.e. is immutable.

If the objects are large and/or many there a significant performance impact may
raise from a deepcopy or manual cloning of objects. Therefore, the
``isimmutable()`` built-in function is key for a safe implementation of
reference-instead-of-copying.

Ensuring immutability is also key for the so called 'Flyweight Design Pattern'.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

'isimmutable' and 'ImmutableNester' Frank-Rene Schäfer <fschaef@gmail.com> - 2013-11-11 21:47 +0100
  Re: 'isimmutable' and 'ImmutableNester' Ned Batchelder <ned@nedbatchelder.com> - 2013-11-11 12:55 -0800
    Re: 'isimmutable' and 'ImmutableNester' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-12 00:17 +0000
      Re: 'isimmutable' and 'ImmutableNester' Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-12 00:30 +0000
      Re: 'isimmutable' and 'ImmutableNester' Chris Angelico <rosuav@gmail.com> - 2013-11-12 12:35 +1100
  Re: 'isimmutable' and 'ImmutableNester' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-12 01:38 +0000

csiph-web