Skip to content

Commit 1ac69e0

Browse files
committed
shallow/deep copy; mutable values; clarify PEP 603
1 parent e5b7f89 commit 1ac69e0

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

peps/pep-0814.rst

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ Construction
8080
* ``frozendict(collection, **kwargs)`` combines the two previous
8181
constructions.
8282

83+
Keys must be hashable and so immutable, but values can be mutable.
84+
Using immutable values creates a hashtable ``frozendict``.
85+
86+
Creating a ``frozendict`` from a ``dict``, ``frozendict(dict)``, has a
87+
complexity of *O*\ (*n*): items are copied (shallow copy).
88+
8389
The insertion order is preserved.
8490

8591

@@ -155,6 +161,29 @@ creates a new ``frozendict``::
155161
See also :pep:`584` "Add Union Operators To dict".
156162

157163

164+
Copy
165+
----
166+
167+
``frozencopy.copy()`` returns a shallow copy. In CPython, it simply
168+
returns the same ``frozendict`` (new reference).
169+
170+
Use ``copy.deepcopy()`` to get a deep copy.
171+
172+
Example::
173+
174+
>>> import copy
175+
>>> d = frozendict(x=[])
176+
>>> shallow_copy = d.copy()
177+
>>> deep_copy = copy.deepcopy(d)
178+
>>> d['x'].append(4)
179+
>>> d
180+
frozendict({'x': [4]})
181+
>>> shallow_copy # modified!
182+
frozendict({'x': [4]})
183+
>>> deep_copy # unchanged
184+
frozendict({'x': []})
185+
186+
158187
Typing
159188
------
160189

@@ -311,12 +340,10 @@ Relationship to PEP 603 frozenmap
311340
* ``excluding(key)``
312341
* ``union(mapping=None, **kw)``
313342

314-
========== ============== ==============
315-
Complexity ``frozenmap`` ``frozendict``
316-
========== ============== ==============
317-
Lookup *O*\ (log *n*) *O*\ (1)
318-
Copy *O*\ (1) *O*\ (*n*)
319-
========== ============== ==============
343+
These methods to mutate a ``frozenmap`` have a complexity of *O*\ (1).
344+
345+
* A mapping lookup (``mapping[key]``) has a complexity of *O*\ (log *n*)
346+
with ``frozenmap`` and a complexity of *O*\ (1) with ``frozendict``.
320347

321348

322349
Reference Implementation

0 commit comments

Comments
 (0)