@@ -38,8 +38,8 @@ It is conceivable that third party objects also have similar mutable
38
38
and immutable counterparts, and it would be useful to have a standard
39
39
protocol for conversion of such objects.
40
40
41
- sets.Set objects expose a "protocol for automatic conversion to
42
- immutable" so that you can create sets.Sets of sets.Sets . :pep: `218 `
41
+ `` sets.Set `` objects expose a "protocol for automatic conversion to
42
+ immutable" so that you can create `` sets.Set ``'s of `` sets.Set ``'s . :pep: `218 `
43
43
deliberately dropped this feature from built-in sets. This PEP
44
44
advances that the feature is still useful and proposes a standard
45
45
mechanism for its support.
@@ -48,22 +48,24 @@ mechanism for its support.
48
48
Proposal
49
49
========
50
50
51
- It is proposed that a new built-in function called freeze() is added.
51
+ It is proposed that a new built-in function called `` freeze() `` is added.
52
52
53
- If freeze() is passed an immutable object, as determined by hash() on
54
- that object not raising a TypeError, then the object is returned
53
+ If `` freeze() `` is passed an immutable object, as determined by `` hash() `` on
54
+ that object not raising a `` TypeError `` , then the object is returned
55
55
directly.
56
56
57
- If freeze() is passed a mutable object (i.e. hash() of that object
58
- raises a TypeError), then freeze() will call that object's
59
- __freeze__() method to get an immutable copy. If the object does not
60
- have a __freeze__() method, then a TypeError is raised.
57
+ If `` freeze() `` is passed a mutable object (i.e. `` hash() `` of that object
58
+ raises a `` TypeError `` ), then `` freeze() `` will call that object's
59
+ `` __freeze__() `` method to get an immutable copy. If the object does not
60
+ have a `` __freeze__() `` method, then a `` TypeError `` is raised.
61
61
62
62
63
63
Sample implementations
64
64
======================
65
65
66
- Here is a Python implementation of the freeze() built-in::
66
+ Here is a Python implementation of the ``freeze() `` built-in:
67
+
68
+ .. code-block :: python
67
69
68
70
def freeze (obj ):
69
71
try :
@@ -73,9 +75,11 @@ Here is a Python implementation of the freeze() built-in::
73
75
freezer = getattr (obj, ' __freeze__' , None )
74
76
if freezer:
75
77
return freezer()
76
- raise TypeError('object is not freezable')``
78
+ raise TypeError (' object is not freezable' )
79
+
80
+ Here are some code samples which show the intended semantics:
77
81
78
- Here are some code samples which show the intended semantics::
82
+ .. code-block :: python
79
83
80
84
class xset (set ):
81
85
def __freeze__ (self ):
@@ -104,6 +108,8 @@ Here are some code samples which show the intended semantics::
104
108
def __freeze__ (self ):
105
109
return imdict(self )
106
110
111
+ .. code-block :: python-console
112
+
107
113
>>> s = set([1, 2, 3])
108
114
>>> {s: 4}
109
115
Traceback (most recent call last):
@@ -140,9 +146,9 @@ Reference implementation
140
146
========================
141
147
142
148
Patch 1335812 _ provides the C implementation of this feature. It adds the
143
- freeze() built-in, along with implementations of the __freeze__()
149
+ `` freeze() `` built-in, along with implementations of the `` __freeze__() ``
144
150
method for lists and sets. Dictionaries are not easily freezable in
145
- current Python, so an implementation of dict.__freeze__() is not
151
+ current Python, so an implementation of `` dict.__freeze__() `` is not
146
152
provided yet.
147
153
148
154
.. _1335812 : http://sourceforge.net/tracker/index.php?func=detail&aid=1335812&group_id=5470&atid=305470
@@ -155,11 +161,11 @@ Open issues
155
161
- Should dicts and sets automatically freeze their mutable keys?
156
162
157
163
- Should we support "temporary freezing" (perhaps with a method called
158
- __congeal__()) a la __as_temporarily_immutable__() in sets.Set?
164
+ `` __congeal__() `` ) a la `` __as_temporarily_immutable__() `` in `` sets.Set `` ?
159
165
160
- - For backward compatibility with sets.Set, should we support
161
- __as_immutable__()? Or should __freeze__() just be renamed to
162
- __as_immutable__()?
166
+ - For backward compatibility with `` sets.Set `` , should we support
167
+ `` __as_immutable__() `` ? Or should `` __freeze__() `` just be renamed to
168
+ `` __as_immutable__() `` ?
163
169
164
170
165
171
Copyright
0 commit comments