Skip to content

Commit a959610

Browse files
filiplajszczakcaseneuve
authored andcommitted
gh-96408: Document difference between set-like view and sets.
While using set operators, set-like views accept any iterable as the other operand, unlike sets which only accept sets as the input. Co-authored-by: Filip Łajszczak <filip@lajszczak.dev>
1 parent f49dd54 commit a959610

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Doc/library/stdtypes.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4692,15 +4692,19 @@ support membership tests:
46924692
Keys views are set-like since their entries are unique and hashable. If all
46934693
values are hashable, so that ``(key, value)`` pairs are unique and hashable,
46944694
then the items view is also set-like. (Values views are not treated as set-like
4695-
since the entries are generally not unique.) For set-like views, all of the
4696-
operations defined for the abstract base class :class:`collections.abc.Set` are
4697-
available (for example, ``==``, ``<``, or ``^``).
4695+
since the entries are generally not unique.)
4696+
4697+
For set-like views, all of the operations defined for the abstract base class
4698+
:class:`collections.abc.Set` are available (for example, ``==``, ``<``, or
4699+
``^``). The difference between sets and set-like views is that unlike sets,
4700+
dictionary views accept any iterable as the other operand.
46984701

46994702
An example of dictionary view usage::
47004703

47014704
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
47024705
>>> keys = dishes.keys()
47034706
>>> values = dishes.values()
4707+
>>> items = dishes.items()
47044708

47054709
>>> # iteration
47064710
>>> n = 0
@@ -4727,6 +4731,12 @@ An example of dictionary view usage::
47274731
>>> keys ^ {'sausage', 'juice'}
47284732
{'juice', 'sausage', 'bacon', 'spam'}
47294733

4734+
>>> # set operations against iterables
4735+
>>> keys | ['juice', 'juice', 'juice']
4736+
{'juice', 'sausage', 'bacon', 'spam', 'eggs'}
4737+
>>> items ^ (('sausage', 1), ('juice', 3))
4738+
{('bacon', 1), ('eggs', 2), ('juice', 3), ('spam', 500)}
4739+
47304740
>>> # get back a read-only proxy for the original dictionary
47314741
>>> values.mapping
47324742
mappingproxy({'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500})

0 commit comments

Comments
 (0)