-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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:: | ||||||||||
|
||||||||||
>>> {0, True, False, 1} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
Suggested change
|
||||||||||
{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. | ||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
.. class:: dict(**kwargs) | ||||||||||
dict(mapping, **kwargs) | ||||||||||
dict(iterable, **kwargs) | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not true.