Skip to content

gh-77753: Add hash examples for set/dict #92549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4181,6 +4181,12 @@ another set. The :class:`frozenset` type is immutable and :term:`hashable` ---
its contents cannot be altered after it is created; it can therefore be used as
a dictionary key or as an element of another set.

Note that as long as the objects have the same hash values, they are seen as
equivalent by sets::
Comment on lines +4184 to +4185
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not true.

>>> hash(1) == hash(2**61)
True
>>> {1, 2**61}
{1, 2305843009213693952}


>>> {0, True, False, 1}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add some of the types Terry listed in the issue too? I was frankly surprised at them (especially the Decimal and Fractions with the same hash value).
Maybe this would help:

Suggested change
>>> {0, True, False, 1}
>>> {0, True, False, 1, 0.0, 1.0}

{0, True}

Non-empty sets (not frozensets) can be created by placing a comma-separated list
of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the
:class:`set` constructor.
Expand Down Expand Up @@ -4383,6 +4389,20 @@ then they can be used interchangeably to index the same dictionary entry. (Note
however, that since computers store floating-point numbers as approximations it
is usually unwise to use them as dictionary keys.)

This also means if two keys have the same hash values, they will map to the same
entry in the dictionary::
Comment on lines +4392 to +4393
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.


>>> {0: 0, False: 0}
{0: 0}

However, note that when a key with the same hash as an existing key is inserted
into a dictionary, it replaces the value but not the key::

>>> d = {False: 'false'}
>>> d[0] = 0
>>> d
{False: 0}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{False: 0}
{False: 0}
>>> {1: 'a', True: 'b'}
{1: 'b'}


.. class:: dict(**kwargs)
dict(mapping, **kwargs)
dict(iterable, **kwargs)
Expand Down