Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 1 | \section{\module{sets} --- |
| 2 | Unordered collections of unique elements} |
| 3 | |
| 4 | \declaremodule{standard}{sets} |
| 5 | \modulesynopsis{Implementation of sets of unique elements.} |
| 6 | \moduleauthor{Greg V. Wilson}{gvwilson@nevex.com} |
| 7 | \moduleauthor{Alex Martelli}{aleax@aleax.it} |
| 8 | \moduleauthor{Guido van Rossum}{guido@python.org} |
| 9 | \sectionauthor{Raymond D. Hettinger}{python@rcn.com} |
| 10 | |
| 11 | \versionadded{2.3} |
Georg Brandl | 7ded34d | 2007-01-13 12:31:51 +0000 | [diff] [blame] | 12 | \deprecated{2.6}{The built-in \code{set}/\code{frozenset} types replace this |
Brett Cannon | 093b670 | 2007-01-13 00:29:49 +0000 | [diff] [blame] | 13 | module.} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 14 | |
| 15 | The \module{sets} module provides classes for constructing and manipulating |
| 16 | unordered collections of unique elements. Common uses include membership |
| 17 | testing, removing duplicates from a sequence, and computing standard math |
| 18 | operations on sets such as intersection, union, difference, and symmetric |
| 19 | difference. |
| 20 | |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 21 | Like other collections, sets support \code{\var{x} in \var{set}}, |
| 22 | \code{len(\var{set})}, and \code{for \var{x} in \var{set}}. Being an |
| 23 | unordered collection, sets do not record element position or order of |
| 24 | insertion. Accordingly, sets do not support indexing, slicing, or |
| 25 | other sequence-like behavior. |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 26 | |
| 27 | Most set applications use the \class{Set} class which provides every set |
| 28 | method except for \method{__hash__()}. For advanced applications requiring |
| 29 | a hash method, the \class{ImmutableSet} class adds a \method{__hash__()} |
| 30 | method but omits methods which alter the contents of the set. Both |
| 31 | \class{Set} and \class{ImmutableSet} derive from \class{BaseSet}, an |
| 32 | abstract class useful for determining whether something is a set: |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 33 | \code{isinstance(\var{obj}, BaseSet)}. |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 34 | |
Raymond Hettinger | e490502 | 2005-04-10 17:32:35 +0000 | [diff] [blame] | 35 | The set classes are implemented using dictionaries. Accordingly, the |
| 36 | requirements for set elements are the same as those for dictionary keys; |
| 37 | namely, that the element defines both \method{__eq__} and \method{__hash__}. |
| 38 | As a result, sets |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 39 | cannot contain mutable elements such as lists or dictionaries. |
| 40 | However, they can contain immutable collections such as tuples or |
Raymond Hettinger | fa8dd5f | 2002-08-23 18:10:54 +0000 | [diff] [blame] | 41 | instances of \class{ImmutableSet}. For convenience in implementing |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 42 | sets of sets, inner sets are automatically converted to immutable |
| 43 | form, for example, \code{Set([Set(['dog'])])} is transformed to |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 44 | \code{Set([ImmutableSet(['dog'])])}. |
| 45 | |
| 46 | \begin{classdesc}{Set}{\optional{iterable}} |
| 47 | Constructs a new empty \class{Set} object. If the optional \var{iterable} |
| 48 | parameter is supplied, updates the set with elements obtained from iteration. |
| 49 | All of the elements in \var{iterable} should be immutable or be transformable |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 50 | to an immutable using the protocol described in |
| 51 | section~\ref{immutable-transforms}. |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 52 | \end{classdesc} |
| 53 | |
| 54 | \begin{classdesc}{ImmutableSet}{\optional{iterable}} |
| 55 | Constructs a new empty \class{ImmutableSet} object. If the optional |
| 56 | \var{iterable} parameter is supplied, updates the set with elements obtained |
| 57 | from iteration. All of the elements in \var{iterable} should be immutable or |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 58 | be transformable to an immutable using the protocol described in |
| 59 | section~\ref{immutable-transforms}. |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 60 | |
| 61 | Because \class{ImmutableSet} objects provide a \method{__hash__()} method, |
| 62 | they can be used as set elements or as dictionary keys. \class{ImmutableSet} |
| 63 | objects do not have methods for adding or removing elements, so all of the |
| 64 | elements must be known when the constructor is called. |
| 65 | \end{classdesc} |
| 66 | |
| 67 | |
Fred Drake | 2e3ae21 | 2003-01-06 15:50:32 +0000 | [diff] [blame] | 68 | \subsection{Set Objects \label{set-objects}} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 69 | |
| 70 | Instances of \class{Set} and \class{ImmutableSet} both provide |
| 71 | the following operations: |
| 72 | |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 73 | \begin{tableiii}{c|c|l}{code}{Operation}{Equivalent}{Result} |
| 74 | \lineiii{len(\var{s})}{}{cardinality of set \var{s}} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 75 | |
| 76 | \hline |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 77 | \lineiii{\var{x} in \var{s}}{} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 78 | {test \var{x} for membership in \var{s}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 79 | \lineiii{\var{x} not in \var{s}}{} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 80 | {test \var{x} for non-membership in \var{s}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 81 | \lineiii{\var{s}.issubset(\var{t})}{\code{\var{s} <= \var{t}}} |
| 82 | {test whether every element in \var{s} is in \var{t}} |
| 83 | \lineiii{\var{s}.issuperset(\var{t})}{\code{\var{s} >= \var{t}}} |
| 84 | {test whether every element in \var{t} is in \var{s}} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 85 | |
| 86 | \hline |
Fred Drake | 447083e | 2005-01-19 07:24:34 +0000 | [diff] [blame] | 87 | \lineiii{\var{s}.union(\var{t})}{\var{s} \textbar{} \var{t}} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 88 | {new set with elements from both \var{s} and \var{t}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 89 | \lineiii{\var{s}.intersection(\var{t})}{\var{s} \&\ \var{t}} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 90 | {new set with elements common to \var{s} and \var{t}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 91 | \lineiii{\var{s}.difference(\var{t})}{\var{s} - \var{t}} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 92 | {new set with elements in \var{s} but not in \var{t}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 93 | \lineiii{\var{s}.symmetric_difference(\var{t})}{\var{s} \^\ \var{t}} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 94 | {new set with elements in either \var{s} or \var{t} but not both} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 95 | \lineiii{\var{s}.copy()}{} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 96 | {new set with a shallow copy of \var{s}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 97 | \end{tableiii} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 98 | |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 99 | Note, the non-operator versions of \method{union()}, |
Raymond Hettinger | 6a18012 | 2003-08-17 08:34:09 +0000 | [diff] [blame] | 100 | \method{intersection()}, \method{difference()}, and |
| 101 | \method{symmetric_difference()} will accept any iterable as an argument. |
| 102 | In contrast, their operator based counterparts require their arguments to |
| 103 | be sets. This precludes error-prone constructions like |
| 104 | \code{Set('abc') \&\ 'cbs'} in favor of the more readable |
| 105 | \code{Set('abc').intersection('cbs')}. |
| 106 | \versionchanged[Formerly all arguments were required to be sets]{2.3.1} |
| 107 | |
Tim Peters | ea76c98 | 2002-08-25 18:43:10 +0000 | [diff] [blame] | 108 | In addition, both \class{Set} and \class{ImmutableSet} |
| 109 | support set to set comparisons. Two sets are equal if and only if |
| 110 | every element of each set is contained in the other (each is a subset |
| 111 | of the other). |
| 112 | A set is less than another set if and only if the first set is a proper |
| 113 | subset of the second set (is a subset, but is not equal). |
| 114 | A set is greater than another set if and only if the first set is a proper |
| 115 | superset of the second set (is a superset, but is not equal). |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 116 | |
Raymond Hettinger | 3801ec7 | 2003-01-15 15:46:05 +0000 | [diff] [blame] | 117 | The subset and equality comparisons do not generalize to a complete |
| 118 | ordering function. For example, any two disjoint sets are not equal and |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 119 | are not subsets of each other, so \emph{all} of the following return |
| 120 | \code{False}: \code{\var{a}<\var{b}}, \code{\var{a}==\var{b}}, or |
| 121 | \code{\var{a}>\var{b}}. |
Raymond Hettinger | 3801ec7 | 2003-01-15 15:46:05 +0000 | [diff] [blame] | 122 | Accordingly, sets do not implement the \method{__cmp__} method. |
| 123 | |
| 124 | Since sets only define partial ordering (subset relationships), the output |
| 125 | of the \method{list.sort()} method is undefined for lists of sets. |
| 126 | |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 127 | The following table lists operations available in \class{ImmutableSet} |
| 128 | but not found in \class{Set}: |
| 129 | |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 130 | \begin{tableii}{c|l}{code}{Operation}{Result} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 131 | \lineii{hash(\var{s})}{returns a hash value for \var{s}} |
| 132 | \end{tableii} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 133 | |
| 134 | The following table lists operations available in \class{Set} |
| 135 | but not found in \class{ImmutableSet}: |
| 136 | |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 137 | \begin{tableiii}{c|c|l}{code}{Operation}{Equivalent}{Result} |
Raymond Hettinger | 16ffbc3 | 2005-07-01 23:00:13 +0000 | [diff] [blame] | 138 | \lineiii{\var{s}.update(\var{t})} |
Fred Drake | 447083e | 2005-01-19 07:24:34 +0000 | [diff] [blame] | 139 | {\var{s} \textbar= \var{t}} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 140 | {return set \var{s} with elements added from \var{t}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 141 | \lineiii{\var{s}.intersection_update(\var{t})} |
| 142 | {\var{s} \&= \var{t}} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 143 | {return set \var{s} keeping only elements also found in \var{t}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 144 | \lineiii{\var{s}.difference_update(\var{t})} |
| 145 | {\var{s} -= \var{t}} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 146 | {return set \var{s} after removing elements found in \var{t}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 147 | \lineiii{\var{s}.symmetric_difference_update(\var{t})} |
| 148 | {\var{s} \textasciicircum= \var{t}} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 149 | {return set \var{s} with elements from \var{s} or \var{t} |
| 150 | but not both} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 151 | |
| 152 | \hline |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 153 | \lineiii{\var{s}.add(\var{x})}{} |
Fred Drake | 2e3ae21 | 2003-01-06 15:50:32 +0000 | [diff] [blame] | 154 | {add element \var{x} to set \var{s}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 155 | \lineiii{\var{s}.remove(\var{x})}{} |
Georg Brandl | db815ab | 2006-03-17 16:26:31 +0000 | [diff] [blame] | 156 | {remove \var{x} from set \var{s}; raises \exception{KeyError} |
| 157 | if not present} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 158 | \lineiii{\var{s}.discard(\var{x})}{} |
Fred Drake | 2e3ae21 | 2003-01-06 15:50:32 +0000 | [diff] [blame] | 159 | {removes \var{x} from set \var{s} if present} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 160 | \lineiii{\var{s}.pop()}{} |
| 161 | {remove and return an arbitrary element from \var{s}; raises |
Georg Brandl | db815ab | 2006-03-17 16:26:31 +0000 | [diff] [blame] | 162 | \exception{KeyError} if empty} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 163 | \lineiii{\var{s}.clear()}{} |
Fred Drake | 2e3ae21 | 2003-01-06 15:50:32 +0000 | [diff] [blame] | 164 | {remove all elements from set \var{s}} |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 165 | \end{tableiii} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 166 | |
Raymond Hettinger | 16ffbc3 | 2005-07-01 23:00:13 +0000 | [diff] [blame] | 167 | Note, the non-operator versions of \method{update()}, |
Raymond Hettinger | 6a18012 | 2003-08-17 08:34:09 +0000 | [diff] [blame] | 168 | \method{intersection_update()}, \method{difference_update()}, and |
| 169 | \method{symmetric_difference_update()} will accept any iterable as |
| 170 | an argument. |
| 171 | \versionchanged[Formerly all arguments were required to be sets]{2.3.1} |
| 172 | |
Raymond Hettinger | 16ffbc3 | 2005-07-01 23:00:13 +0000 | [diff] [blame] | 173 | Also note, the module also includes a \method{union_update()} method |
| 174 | which is an alias for \method{update()}. The method is included for |
Georg Brandl | 08c02db | 2005-07-22 18:39:19 +0000 | [diff] [blame] | 175 | backwards compatibility. Programmers should prefer the |
Neal Norwitz | 26f4c23 | 2005-11-03 04:39:09 +0000 | [diff] [blame] | 176 | \method{update()} method because it is supported by the builtin |
Raymond Hettinger | 16ffbc3 | 2005-07-01 23:00:13 +0000 | [diff] [blame] | 177 | \class{set()} and \class{frozenset()} types. |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 178 | |
Fred Drake | 2e3ae21 | 2003-01-06 15:50:32 +0000 | [diff] [blame] | 179 | \subsection{Example \label{set-example}} |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 180 | |
| 181 | \begin{verbatim} |
| 182 | >>> from sets import Set |
| 183 | >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice']) |
| 184 | >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice']) |
Raymond Hettinger | 7ceb29e | 2003-08-16 00:56:40 +0000 | [diff] [blame] | 185 | >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack']) |
| 186 | >>> employees = engineers | programmers | managers # union |
| 187 | >>> engineering_management = engineers & managers # intersection |
| 188 | >>> fulltime_management = managers - engineers - programmers # difference |
| 189 | >>> engineers.add('Marvin') # add element |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 190 | >>> print engineers |
| 191 | Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack']) |
| 192 | >>> employees.issuperset(engineers) # superset test |
| 193 | False |
Raymond Hettinger | 6a18012 | 2003-08-17 08:34:09 +0000 | [diff] [blame] | 194 | >>> employees.union_update(engineers) # update from another set |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 195 | >>> employees.issuperset(engineers) |
| 196 | True |
Raymond Hettinger | d456849 | 2003-11-16 13:44:19 +0000 | [diff] [blame] | 197 | >>> for group in [engineers, programmers, managers, employees]: |
Raymond Hettinger | 3801ec7 | 2003-01-15 15:46:05 +0000 | [diff] [blame] | 198 | ... group.discard('Susan') # unconditionally remove element |
| 199 | ... print group |
| 200 | ... |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 201 | Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack']) |
| 202 | Set(['Janice', 'Jack', 'Sam']) |
| 203 | Set(['Jane', 'Zack', 'Jack']) |
| 204 | Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack']) |
| 205 | \end{verbatim} |
| 206 | |
| 207 | |
| 208 | \subsection{Protocol for automatic conversion to immutable |
| 209 | \label{immutable-transforms}} |
| 210 | |
| 211 | Sets can only contain immutable elements. For convenience, mutable |
| 212 | \class{Set} objects are automatically copied to an \class{ImmutableSet} |
| 213 | before being added as a set element. |
| 214 | |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 215 | The mechanism is to always add a hashable element, or if it is not |
| 216 | hashable, the element is checked to see if it has an |
Raymond Hettinger | 2835e37 | 2003-02-14 03:42:11 +0000 | [diff] [blame] | 217 | \method{__as_immutable__()} method which returns an immutable equivalent. |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 218 | |
Raymond Hettinger | 2835e37 | 2003-02-14 03:42:11 +0000 | [diff] [blame] | 219 | Since \class{Set} objects have a \method{__as_immutable__()} method |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 220 | returning an instance of \class{ImmutableSet}, it is possible to |
| 221 | construct sets of sets. |
| 222 | |
| 223 | A similar mechanism is needed by the \method{__contains__()} and |
| 224 | \method{remove()} methods which need to hash an element to check |
| 225 | for membership in a set. Those methods check an element for hashability |
Raymond Hettinger | 2835e37 | 2003-02-14 03:42:11 +0000 | [diff] [blame] | 226 | and, if not, check for a \method{__as_temporarily_immutable__()} method |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 227 | which returns the element wrapped by a class that provides temporary |
| 228 | methods for \method{__hash__()}, \method{__eq__()}, and \method{__ne__()}. |
| 229 | |
| 230 | The alternate mechanism spares the need to build a separate copy of |
| 231 | the original mutable object. |
| 232 | |
Raymond Hettinger | 2835e37 | 2003-02-14 03:42:11 +0000 | [diff] [blame] | 233 | \class{Set} objects implement the \method{__as_temporarily_immutable__()} |
Fred Drake | d10c6c9 | 2002-08-23 17:22:36 +0000 | [diff] [blame] | 234 | method which returns the \class{Set} object wrapped by a new class |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 235 | \class{_TemporarilyImmutableSet}. |
| 236 | |
| 237 | The two mechanisms for adding hashability are normally invisible to the |
| 238 | user; however, a conflict can arise in a multi-threaded environment |
Raymond Hettinger | fa8dd5f | 2002-08-23 18:10:54 +0000 | [diff] [blame] | 239 | where one thread is updating a set while another has temporarily wrapped it |
Raymond Hettinger | 584cb19 | 2002-08-23 15:18:38 +0000 | [diff] [blame] | 240 | in \class{_TemporarilyImmutableSet}. In other words, sets of mutable sets |
| 241 | are not thread-safe. |
Raymond Hettinger | 16ffbc3 | 2005-07-01 23:00:13 +0000 | [diff] [blame] | 242 | |
| 243 | |
| 244 | \subsection{Comparison to the built-in \class{set} types |
| 245 | \label{comparison-to-builtin-set}} |
| 246 | |
| 247 | The built-in \class{set} and \class{frozenset} types were designed based |
| 248 | on lessons learned from the \module{sets} module. The key differences are: |
| 249 | |
| 250 | \begin{itemize} |
| 251 | \item \class{Set} and \class{ImmutableSet} were renamed to \class{set} and |
| 252 | \class{frozenset}. |
| 253 | \item There is no equivalent to \class{BaseSet}. Instead, use |
| 254 | \code{isinstance(x, (set, frozenset))}. |
| 255 | \item The hash algorithm for the built-ins performs significantly better |
| 256 | (fewer collisions) for most datasets. |
| 257 | \item The built-in versions have more space efficient pickles. |
| 258 | \item The built-in versions do not have a \method{union_update()} method. |
| 259 | Instead, use the \method{update()} method which is equivalent. |
Georg Brandl | 08c02db | 2005-07-22 18:39:19 +0000 | [diff] [blame] | 260 | \item The built-in versions do not have a \method{_repr(sorted=True)} method. |
Raymond Hettinger | 16ffbc3 | 2005-07-01 23:00:13 +0000 | [diff] [blame] | 261 | Instead, use the built-in \function{repr()} and \function{sorted()} |
| 262 | functions: \code{repr(sorted(s))}. |
| 263 | \item The built-in version does not have a protocol for automatic conversion |
| 264 | to immutable. Many found this feature to be confusing and no one |
| 265 | in the community reported having found real uses for it. |
| 266 | \end{itemize} |