mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-101100: Fix references to the set methods (GH-141857) (GH-142300)
gh-101100: Fix references to the set methods (GH-141857)
(cherry picked from commit 1d8f3ed2eb)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
0e4f4f1a46
commit
c9b2ec6c66
4 changed files with 113 additions and 105 deletions
|
|
@ -147,7 +147,7 @@ subtypes but not for instances of :class:`frozenset` or its subtypes.
|
||||||
|
|
||||||
Return ``1`` if found and removed, ``0`` if not found (no action taken), and ``-1`` if an
|
Return ``1`` if found and removed, ``0`` if not found (no action taken), and ``-1`` if an
|
||||||
error is encountered. Does not raise :exc:`KeyError` for missing keys. Raise a
|
error is encountered. Does not raise :exc:`KeyError` for missing keys. Raise a
|
||||||
:exc:`TypeError` if the *key* is unhashable. Unlike the Python :meth:`~frozenset.discard`
|
:exc:`TypeError` if the *key* is unhashable. Unlike the Python :meth:`~set.discard`
|
||||||
method, this function does not automatically convert unhashable sets into
|
method, this function does not automatically convert unhashable sets into
|
||||||
temporary frozensets. Raise :exc:`SystemError` if *set* is not an
|
temporary frozensets. Raise :exc:`SystemError` if *set* is not an
|
||||||
instance of :class:`set` or its subtype.
|
instance of :class:`set` or its subtype.
|
||||||
|
|
|
||||||
|
|
@ -4802,7 +4802,7 @@ other sequence-like behavior.
|
||||||
|
|
||||||
There are currently two built-in set types, :class:`set` and :class:`frozenset`.
|
There are currently two built-in set types, :class:`set` and :class:`frozenset`.
|
||||||
The :class:`set` type is mutable --- the contents can be changed using methods
|
The :class:`set` type is mutable --- the contents can be changed using methods
|
||||||
like :meth:`add <frozenset.add>` and :meth:`remove <frozenset.add>`.
|
like :meth:`~set.add` and :meth:`~set.remove`.
|
||||||
Since it is mutable, it has no hash value and cannot be used as
|
Since it is mutable, it has no hash value and cannot be used as
|
||||||
either a dictionary key or as an element of another set.
|
either a dictionary key or as an element of another set.
|
||||||
The :class:`frozenset` type is immutable and :term:`hashable` ---
|
The :class:`frozenset` type is immutable and :term:`hashable` ---
|
||||||
|
|
@ -4824,164 +4824,172 @@ The constructors for both classes work the same:
|
||||||
objects. If *iterable* is not specified, a new empty set is
|
objects. If *iterable* is not specified, a new empty set is
|
||||||
returned.
|
returned.
|
||||||
|
|
||||||
Sets can be created by several means:
|
Sets can be created by several means:
|
||||||
|
|
||||||
* Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}``
|
* Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}``
|
||||||
* Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}``
|
* Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}``
|
||||||
* Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])``
|
* Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])``
|
||||||
|
|
||||||
Instances of :class:`set` and :class:`frozenset` provide the following
|
Instances of :class:`set` and :class:`frozenset` provide the following
|
||||||
operations:
|
operations:
|
||||||
|
|
||||||
.. describe:: len(s)
|
.. describe:: len(s)
|
||||||
|
|
||||||
Return the number of elements in set *s* (cardinality of *s*).
|
Return the number of elements in set *s* (cardinality of *s*).
|
||||||
|
|
||||||
.. describe:: x in s
|
.. describe:: x in s
|
||||||
|
|
||||||
Test *x* for membership in *s*.
|
Test *x* for membership in *s*.
|
||||||
|
|
||||||
.. describe:: x not in s
|
.. describe:: x not in s
|
||||||
|
|
||||||
Test *x* for non-membership in *s*.
|
Test *x* for non-membership in *s*.
|
||||||
|
|
||||||
.. method:: isdisjoint(other, /)
|
.. method:: frozenset.isdisjoint(other, /)
|
||||||
|
set.isdisjoint(other, /)
|
||||||
|
|
||||||
Return ``True`` if the set has no elements in common with *other*. Sets are
|
Return ``True`` if the set has no elements in common with *other*. Sets are
|
||||||
disjoint if and only if their intersection is the empty set.
|
disjoint if and only if their intersection is the empty set.
|
||||||
|
|
||||||
.. method:: issubset(other, /)
|
.. method:: frozenset.issubset(other, /)
|
||||||
set <= other
|
set.issubset(other, /)
|
||||||
|
.. describe:: set <= other
|
||||||
|
|
||||||
Test whether every element in the set is in *other*.
|
Test whether every element in the set is in *other*.
|
||||||
|
|
||||||
.. method:: set < other
|
.. describe:: set < other
|
||||||
|
|
||||||
Test whether the set is a proper subset of *other*, that is,
|
Test whether the set is a proper subset of *other*, that is,
|
||||||
``set <= other and set != other``.
|
``set <= other and set != other``.
|
||||||
|
|
||||||
.. method:: issuperset(other, /)
|
.. method:: frozenset.issuperset(other, /)
|
||||||
set >= other
|
set.issuperset(other, /)
|
||||||
|
.. describe:: set >= other
|
||||||
|
|
||||||
Test whether every element in *other* is in the set.
|
Test whether every element in *other* is in the set.
|
||||||
|
|
||||||
.. method:: set > other
|
.. describe:: set > other
|
||||||
|
|
||||||
Test whether the set is a proper superset of *other*, that is, ``set >=
|
Test whether the set is a proper superset of *other*, that is, ``set >=
|
||||||
other and set != other``.
|
other and set != other``.
|
||||||
|
|
||||||
.. method:: union(*others)
|
.. method:: frozenset.union(*others)
|
||||||
set | other | ...
|
set.union(*others)
|
||||||
|
.. describe:: set | other | ...
|
||||||
|
|
||||||
Return a new set with elements from the set and all others.
|
Return a new set with elements from the set and all others.
|
||||||
|
|
||||||
.. method:: intersection(*others)
|
.. method:: frozenset.intersection(*others)
|
||||||
set & other & ...
|
set.intersection(*others)
|
||||||
|
.. describe:: set & other & ...
|
||||||
|
|
||||||
Return a new set with elements common to the set and all others.
|
Return a new set with elements common to the set and all others.
|
||||||
|
|
||||||
.. method:: difference(*others)
|
.. method:: frozenset.difference(*others)
|
||||||
set - other - ...
|
set.difference(*others)
|
||||||
|
.. describe:: set - other - ...
|
||||||
|
|
||||||
Return a new set with elements in the set that are not in the others.
|
Return a new set with elements in the set that are not in the others.
|
||||||
|
|
||||||
.. method:: symmetric_difference(other, /)
|
.. method:: frozenset.symmetric_difference(other, /)
|
||||||
set ^ other
|
set.symmetric_difference(other, /)
|
||||||
|
.. describe:: set ^ other
|
||||||
|
|
||||||
Return a new set with elements in either the set or *other* but not both.
|
Return a new set with elements in either the set or *other* but not both.
|
||||||
|
|
||||||
.. method:: copy()
|
.. method:: frozenset.copy()
|
||||||
|
set.copy()
|
||||||
|
|
||||||
Return a shallow copy of the set.
|
Return a shallow copy of the set.
|
||||||
|
|
||||||
|
|
||||||
Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
|
Note, the non-operator versions of :meth:`~frozenset.union`,
|
||||||
:meth:`difference`, :meth:`symmetric_difference`, :meth:`issubset`, and
|
:meth:`~frozenset.intersection`, :meth:`~frozenset.difference`, :meth:`~frozenset.symmetric_difference`, :meth:`~frozenset.issubset`, and
|
||||||
:meth:`issuperset` methods will accept any iterable as an argument. In
|
:meth:`~frozenset.issuperset` methods will accept any iterable as an argument. In
|
||||||
contrast, their operator based counterparts require their arguments to be
|
contrast, their operator based counterparts require their arguments to be
|
||||||
sets. This precludes error-prone constructions like ``set('abc') & 'cbs'``
|
sets. This precludes error-prone constructions like ``set('abc') & 'cbs'``
|
||||||
in favor of the more readable ``set('abc').intersection('cbs')``.
|
in favor of the more readable ``set('abc').intersection('cbs')``.
|
||||||
|
|
||||||
Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
|
Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
|
||||||
sets are equal if and only if every element of each set is contained in the
|
sets are equal if and only if every element of each set is contained in the
|
||||||
other (each is a subset of the other). A set is less than another set if and
|
other (each is a subset of the other). A set is less than another set if and
|
||||||
only if the first set is a proper subset of the second set (is a subset, but
|
only if the first set is a proper subset of the second set (is a subset, but
|
||||||
is not equal). A set is greater than another set if and only if the first set
|
is not equal). A set is greater than another set if and only if the first set
|
||||||
is a proper superset of the second set (is a superset, but is not equal).
|
is a proper superset of the second set (is a superset, but is not equal).
|
||||||
|
|
||||||
Instances of :class:`set` are compared to instances of :class:`frozenset`
|
Instances of :class:`set` are compared to instances of :class:`frozenset`
|
||||||
based on their members. For example, ``set('abc') == frozenset('abc')``
|
based on their members. For example, ``set('abc') == frozenset('abc')``
|
||||||
returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``.
|
returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``.
|
||||||
|
|
||||||
The subset and equality comparisons do not generalize to a total ordering
|
The subset and equality comparisons do not generalize to a total ordering
|
||||||
function. For example, any two nonempty disjoint sets are not equal and are not
|
function. For example, any two nonempty disjoint sets are not equal and are not
|
||||||
subsets of each other, so *all* of the following return ``False``: ``a<b``,
|
subsets of each other, so *all* of the following return ``False``: ``a<b``,
|
||||||
``a==b``, or ``a>b``.
|
``a==b``, or ``a>b``.
|
||||||
|
|
||||||
Since sets only define partial ordering (subset relationships), the output of
|
Since sets only define partial ordering (subset relationships), the output of
|
||||||
the :meth:`list.sort` method is undefined for lists of sets.
|
the :meth:`list.sort` method is undefined for lists of sets.
|
||||||
|
|
||||||
Set elements, like dictionary keys, must be :term:`hashable`.
|
Set elements, like dictionary keys, must be :term:`hashable`.
|
||||||
|
|
||||||
Binary operations that mix :class:`set` instances with :class:`frozenset`
|
Binary operations that mix :class:`set` instances with :class:`frozenset`
|
||||||
return the type of the first operand. For example: ``frozenset('ab') |
|
return the type of the first operand. For example: ``frozenset('ab') |
|
||||||
set('bc')`` returns an instance of :class:`frozenset`.
|
set('bc')`` returns an instance of :class:`frozenset`.
|
||||||
|
|
||||||
The following table lists operations available for :class:`set` that do not
|
The following table lists operations available for :class:`set` that do not
|
||||||
apply to immutable instances of :class:`frozenset`:
|
apply to immutable instances of :class:`frozenset`:
|
||||||
|
|
||||||
.. method:: update(*others)
|
.. method:: set.update(*others)
|
||||||
set |= other | ...
|
.. describe:: set |= other | ...
|
||||||
|
|
||||||
Update the set, adding elements from all others.
|
Update the set, adding elements from all others.
|
||||||
|
|
||||||
.. method:: intersection_update(*others)
|
.. method:: set.intersection_update(*others)
|
||||||
set &= other & ...
|
.. describe:: set &= other & ...
|
||||||
|
|
||||||
Update the set, keeping only elements found in it and all others.
|
Update the set, keeping only elements found in it and all others.
|
||||||
|
|
||||||
.. method:: difference_update(*others)
|
.. method:: set.difference_update(*others)
|
||||||
set -= other | ...
|
.. describe:: set -= other | ...
|
||||||
|
|
||||||
Update the set, removing elements found in others.
|
Update the set, removing elements found in others.
|
||||||
|
|
||||||
.. method:: symmetric_difference_update(other, /)
|
.. method:: set.symmetric_difference_update(other, /)
|
||||||
set ^= other
|
.. describe:: set ^= other
|
||||||
|
|
||||||
Update the set, keeping only elements found in either set, but not in both.
|
Update the set, keeping only elements found in either set, but not in both.
|
||||||
|
|
||||||
.. method:: add(elem, /)
|
.. method:: set.add(elem, /)
|
||||||
|
|
||||||
Add element *elem* to the set.
|
Add element *elem* to the set.
|
||||||
|
|
||||||
.. method:: remove(elem, /)
|
.. method:: set.remove(elem, /)
|
||||||
|
|
||||||
Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is
|
Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is
|
||||||
not contained in the set.
|
not contained in the set.
|
||||||
|
|
||||||
.. method:: discard(elem, /)
|
.. method:: set.discard(elem, /)
|
||||||
|
|
||||||
Remove element *elem* from the set if it is present.
|
Remove element *elem* from the set if it is present.
|
||||||
|
|
||||||
.. method:: pop()
|
.. method:: set.pop()
|
||||||
|
|
||||||
Remove and return an arbitrary element from the set. Raises
|
Remove and return an arbitrary element from the set. Raises
|
||||||
:exc:`KeyError` if the set is empty.
|
:exc:`KeyError` if the set is empty.
|
||||||
|
|
||||||
.. method:: clear()
|
.. method:: set.clear()
|
||||||
|
|
||||||
Remove all elements from the set.
|
Remove all elements from the set.
|
||||||
|
|
||||||
|
|
||||||
Note, the non-operator versions of the :meth:`update`,
|
Note, the non-operator versions of the :meth:`~set.update`,
|
||||||
:meth:`intersection_update`, :meth:`difference_update`, and
|
:meth:`~set.intersection_update`, :meth:`~set.difference_update`, and
|
||||||
:meth:`symmetric_difference_update` methods will accept any iterable as an
|
:meth:`~set.symmetric_difference_update` methods will accept any iterable as an
|
||||||
argument.
|
argument.
|
||||||
|
|
||||||
Note, the *elem* argument to the :meth:`~object.__contains__`,
|
Note, the *elem* argument to the :meth:`~object.__contains__`,
|
||||||
:meth:`remove`, and
|
:meth:`~set.remove`, and
|
||||||
:meth:`discard` methods may be a set. To support searching for an equivalent
|
:meth:`~set.discard` methods may be a set. To support searching for an equivalent
|
||||||
frozenset, a temporary one is created from *elem*.
|
frozenset, a temporary one is created from *elem*.
|
||||||
|
|
||||||
|
|
||||||
.. _typesmapping:
|
.. _typesmapping:
|
||||||
|
|
|
||||||
|
|
@ -449,7 +449,7 @@ Sets
|
||||||
|
|
||||||
These represent a mutable set. They are created by the built-in :func:`set`
|
These represent a mutable set. They are created by the built-in :func:`set`
|
||||||
constructor and can be modified afterwards by several methods, such as
|
constructor and can be modified afterwards by several methods, such as
|
||||||
:meth:`add <frozenset.add>`.
|
:meth:`~set.add`.
|
||||||
|
|
||||||
|
|
||||||
Frozen sets
|
Frozen sets
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ Here's a simple example::
|
||||||
The union and intersection of sets can be computed with the :meth:`~frozenset.union` and
|
The union and intersection of sets can be computed with the :meth:`~frozenset.union` and
|
||||||
:meth:`~frozenset.intersection` methods; an alternative notation uses the bitwise operators
|
:meth:`~frozenset.intersection` methods; an alternative notation uses the bitwise operators
|
||||||
``&`` and ``|``. Mutable sets also have in-place versions of these methods,
|
``&`` and ``|``. Mutable sets also have in-place versions of these methods,
|
||||||
:meth:`!union_update` and :meth:`~frozenset.intersection_update`. ::
|
:meth:`!union_update` and :meth:`~set.intersection_update`. ::
|
||||||
|
|
||||||
>>> S1 = sets.Set([1,2,3])
|
>>> S1 = sets.Set([1,2,3])
|
||||||
>>> S2 = sets.Set([4,5,6])
|
>>> S2 = sets.Set([4,5,6])
|
||||||
|
|
@ -87,7 +87,7 @@ It's also possible to take the symmetric difference of two sets. This is the
|
||||||
set of all elements in the union that aren't in the intersection. Another way
|
set of all elements in the union that aren't in the intersection. Another way
|
||||||
of putting it is that the symmetric difference contains all elements that are in
|
of putting it is that the symmetric difference contains all elements that are in
|
||||||
exactly one set. Again, there's an alternative notation (``^``), and an
|
exactly one set. Again, there's an alternative notation (``^``), and an
|
||||||
in-place version with the ungainly name :meth:`~frozenset.symmetric_difference_update`. ::
|
in-place version with the ungainly name :meth:`~set.symmetric_difference_update`. ::
|
||||||
|
|
||||||
>>> S1 = sets.Set([1,2,3,4])
|
>>> S1 = sets.Set([1,2,3,4])
|
||||||
>>> S2 = sets.Set([3,4,5,6])
|
>>> S2 = sets.Set([3,4,5,6])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue