F
Frank-Rene Schäfer
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, (e-mail address removed)
* BDFL-Delegate: <PEP czar's real name>
* Discussions-To: (e-mail address removed)
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'.
PEP: <pep number>
Title: ``isimmutable(Obj)`` and/or ``ImmutableNester``
Version: <version string>
Last-Modified: <date string>
Author: Frank-Rene Schaefer, (e-mail address removed)
* BDFL-Delegate: <PEP czar's real name>
* Discussions-To: (e-mail address removed)
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'.