Skip to content

Commit 95a233b

Browse files
committed
Union operators
1 parent 95b64ae commit 95a233b

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

peps/pep-0814.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ It's possible to compare ``frozendict`` to ``dict``. Example::
122122
True
123123

124124

125+
Union operators
126+
---------------
127+
128+
It's possible to join two ``frozendict``, or one ``frozendict`` with a
129+
``dict``, with the merge (``|``) operator. Example::
130+
131+
>>> frozendict(x=1) | frozendict(y=1)
132+
frozendict({'x': 1, 'y': 1})
133+
>>> frozendict(x=1) | dict(y=1)
134+
frozendict({'x': 1, 'y': 1})
135+
136+
If some keys are in common, the values of the right operand are taken::
137+
138+
>>> frozendict(x=1, y=2) | frozendict(y=5)
139+
frozendict({'x': 1, 'y': 5})
140+
141+
The update operator `|=` does not modify a ``frozendict`` in-place, but
142+
creates a new ``frozendict``::
143+
144+
>>> d = frozendict(x=1)
145+
>>> copy = d
146+
>>> d |= frozendict(y=2)
147+
>>> d
148+
frozendict({'x': 1, 'y': 2})
149+
>>> copy # left unchanged
150+
frozendict({'x': 1})
151+
152+
See also :pep:`584` "Add Union Operators To dict".
153+
154+
125155
Typing
126156
------
127157

0 commit comments

Comments
 (0)