Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions peps/pep-0351.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ It is conceivable that third party objects also have similar mutable
and immutable counterparts, and it would be useful to have a standard
protocol for conversion of such objects.

sets.Set objects expose a "protocol for automatic conversion to
immutable" so that you can create sets.Sets of sets.Sets. :pep:`218`
``sets.Set`` objects expose a "protocol for automatic conversion to
immutable" so that you can create ``sets.Set``'s of ``sets.Set``'s. :pep:`218`
deliberately dropped this feature from built-in sets. This PEP
advances that the feature is still useful and proposes a standard
mechanism for its support.
Expand All @@ -48,22 +48,24 @@ mechanism for its support.
Proposal
========

It is proposed that a new built-in function called freeze() is added.
It is proposed that a new built-in function called ``freeze()`` is added.

If freeze() is passed an immutable object, as determined by hash() on
that object not raising a TypeError, then the object is returned
If ``freeze()`` is passed an immutable object, as determined by ``hash()`` on
that object not raising a ``TypeError``, then the object is returned
directly.

If freeze() is passed a mutable object (i.e. hash() of that object
raises a TypeError), then freeze() will call that object's
__freeze__() method to get an immutable copy. If the object does not
have a __freeze__() method, then a TypeError is raised.
If ``freeze()`` is passed a mutable object (i.e. ``hash()`` of that object
raises a ``TypeError``), then ``freeze()`` will call that object's
``__freeze__()`` method to get an immutable copy. If the object does not
have a ``__freeze__()`` method, then a ``TypeError`` is raised.


Sample implementations
======================

Here is a Python implementation of the freeze() built-in::
Here is a Python implementation of the ``freeze()`` built-in:

.. code-block:: python

def freeze(obj):
try:
Expand All @@ -73,9 +75,11 @@ Here is a Python implementation of the freeze() built-in::
freezer = getattr(obj, '__freeze__', None)
if freezer:
return freezer()
raise TypeError('object is not freezable')``
raise TypeError('object is not freezable')

Here are some code samples which show the intended semantics:

Here are some code samples which show the intended semantics::
.. code-block:: python

class xset(set):
def __freeze__(self):
Expand Down Expand Up @@ -104,6 +108,8 @@ Here are some code samples which show the intended semantics::
def __freeze__(self):
return imdict(self)

.. code-block:: python-console

>>> s = set([1, 2, 3])
>>> {s: 4}
Traceback (most recent call last):
Expand Down Expand Up @@ -140,9 +146,9 @@ Reference implementation
========================

Patch 1335812_ provides the C implementation of this feature. It adds the
freeze() built-in, along with implementations of the __freeze__()
``freeze()`` built-in, along with implementations of the ``__freeze__()``
method for lists and sets. Dictionaries are not easily freezable in
current Python, so an implementation of dict.__freeze__() is not
current Python, so an implementation of ``dict.__freeze__()`` is not
provided yet.

.. _1335812: http://sourceforge.net/tracker/index.php?func=detail&aid=1335812&group_id=5470&atid=305470
Expand All @@ -155,11 +161,11 @@ Open issues
- Should dicts and sets automatically freeze their mutable keys?

- Should we support "temporary freezing" (perhaps with a method called
__congeal__()) a la __as_temporarily_immutable__() in sets.Set?
``__congeal__()``) a la ``__as_temporarily_immutable__()`` in ``sets.Set``?

- For backward compatibility with sets.Set, should we support
__as_immutable__()? Or should __freeze__() just be renamed to
__as_immutable__()?
- For backward compatibility with ``sets.Set``, should we support
``__as_immutable__()``? Or should ``__freeze__()`` just be renamed to
``__as_immutable__()``?


Copyright
Expand Down