blob: e4eac7964623596f9871b168a78f42e489adb0e6 [file] [log] [blame]
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001# Copyright 2007 Google, Inc. All Rights Reserved.
2# Licensed to PSF under a Contributor Agreement.
3
4"""Abstract Base Classes (ABCs) for collections, according to PEP 3119.
5
Raymond Hettinger158c9c22011-02-22 00:41:50 +00006Unit tests are in test_collections.
Guido van Rossumcd16bf62007-06-13 18:07:49 +00007"""
8
9from abc import ABCMeta, abstractmethod
Benjamin Peterson41181742008-07-02 20:22:54 +000010import sys
Guido van Rossumcd16bf62007-06-13 18:07:49 +000011
Guido van Rossum48b069a2020-04-07 09:50:06 -070012GenericAlias = type(list[int])
kj463c7d32020-12-14 02:38:24 +080013EllipsisType = type(...)
14def _f(): pass
15FunctionType = type(_f)
16del _f
Guido van Rossum48b069a2020-04-07 09:50:06 -070017
Yury Selivanov22214ab2016-11-16 18:25:04 -050018__all__ = ["Awaitable", "Coroutine",
19 "AsyncIterable", "AsyncIterator", "AsyncGenerator",
Guido van Rossum16ca06b2016-04-04 10:59:29 -070020 "Hashable", "Iterable", "Iterator", "Generator", "Reversible",
Guido van Rossumf0666942016-08-23 10:47:07 -070021 "Sized", "Container", "Callable", "Collection",
Guido van Rossumcd16bf62007-06-13 18:07:49 +000022 "Set", "MutableSet",
23 "Mapping", "MutableMapping",
24 "MappingView", "KeysView", "ItemsView", "ValuesView",
25 "Sequence", "MutableSequence",
Guido van Rossumd05eb002007-11-21 22:26:24 +000026 "ByteString",
Guido van Rossumcd16bf62007-06-13 18:07:49 +000027 ]
28
Christian Heimesbf235bd2013-10-13 02:21:33 +020029# This module has been renamed from collections.abc to _collections_abc to
30# speed up interpreter startup. Some of the types such as MutableMapping are
31# required early but collections module imports a lot of other modules.
32# See issue #19218
33__name__ = "collections.abc"
34
Raymond Hettinger02184282012-04-05 13:31:12 -070035# Private list of types that we want to register with the various ABCs
36# so that they will pass tests like:
37# it = iter(somebytearray)
38# assert isinstance(it, Iterable)
Serhiy Storchaka3bd9fde2016-10-08 21:33:59 +030039# Note: in other implementations, these types might not be distinct
40# and they may have their own implementation specific types that
Raymond Hettinger02184282012-04-05 13:31:12 -070041# are not included on this list.
Christian Heimesf83be4e2007-11-28 09:44:38 +000042bytes_iterator = type(iter(b''))
43bytearray_iterator = type(iter(bytearray()))
44#callable_iterator = ???
45dict_keyiterator = type(iter({}.keys()))
46dict_valueiterator = type(iter({}.values()))
47dict_itemiterator = type(iter({}.items()))
48list_iterator = type(iter([]))
49list_reverseiterator = type(iter(reversed([])))
50range_iterator = type(iter(range(0)))
Serhiy Storchaka48b1c3f2016-10-08 22:04:12 +030051longrange_iterator = type(iter(range(1 << 1000)))
Christian Heimesf83be4e2007-11-28 09:44:38 +000052set_iterator = type(iter(set()))
53str_iterator = type(iter(""))
54tuple_iterator = type(iter(()))
55zip_iterator = type(iter(zip()))
56## views ##
57dict_keys = type({}.keys())
58dict_values = type({}.values())
59dict_items = type({}.items())
Christian Heimes0db38532007-11-29 16:21:13 +000060## misc ##
Victor Stinner7b17a4e2012-04-20 01:41:36 +020061mappingproxy = type(type.__dict__)
Raymond Hettingerbd60e8d2015-05-09 01:07:23 -040062generator = type((lambda: (yield))())
Yury Selivanov5376ba92015-06-22 12:19:30 -040063## coroutine ##
64async def _coro(): pass
65_coro = _coro()
66coroutine = type(_coro)
67_coro.close() # Prevent ResourceWarning
68del _coro
Yury Selivanov22214ab2016-11-16 18:25:04 -050069## asynchronous generator ##
70async def _ag(): yield
71_ag = _ag()
72async_generator = type(_ag)
73del _ag
Christian Heimesf83be4e2007-11-28 09:44:38 +000074
75
Guido van Rossumcd16bf62007-06-13 18:07:49 +000076### ONE-TRICK PONIES ###
77
Guido van Rossum97c1adf2016-08-18 09:22:23 -070078def _check_methods(C, *methods):
79 mro = C.__mro__
80 for method in methods:
81 for B in mro:
82 if method in B.__dict__:
83 if B.__dict__[method] is None:
84 return NotImplemented
85 break
86 else:
87 return NotImplemented
88 return True
89
Guido van Rossumcd16bf62007-06-13 18:07:49 +000090class Hashable(metaclass=ABCMeta):
91
Raymond Hettingerc46759a2011-03-22 11:46:25 -070092 __slots__ = ()
93
Guido van Rossumcd16bf62007-06-13 18:07:49 +000094 @abstractmethod
95 def __hash__(self):
96 return 0
97
98 @classmethod
99 def __subclasshook__(cls, C):
100 if cls is Hashable:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700101 return _check_methods(C, "__hash__")
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000102 return NotImplemented
103
104
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400105class Awaitable(metaclass=ABCMeta):
Yury Selivanov56fc6142015-05-29 09:01:29 -0400106
107 __slots__ = ()
108
109 @abstractmethod
110 def __await__(self):
111 yield
112
113 @classmethod
114 def __subclasshook__(cls, C):
115 if cls is Awaitable:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700116 return _check_methods(C, "__await__")
Yury Selivanov56fc6142015-05-29 09:01:29 -0400117 return NotImplemented
118
Guido van Rossum48b069a2020-04-07 09:50:06 -0700119 __class_getitem__ = classmethod(GenericAlias)
120
Yury Selivanov56fc6142015-05-29 09:01:29 -0400121
122class Coroutine(Awaitable):
Yury Selivanov75445082015-05-11 22:57:16 -0400123
124 __slots__ = ()
125
126 @abstractmethod
127 def send(self, value):
128 """Send a value into the coroutine.
129 Return next yielded value or raise StopIteration.
130 """
131 raise StopIteration
132
133 @abstractmethod
134 def throw(self, typ, val=None, tb=None):
135 """Raise an exception in the coroutine.
136 Return next yielded value or raise StopIteration.
137 """
138 if val is None:
139 if tb is None:
140 raise typ
141 val = typ()
142 if tb is not None:
143 val = val.with_traceback(tb)
144 raise val
145
146 def close(self):
147 """Raise GeneratorExit inside coroutine.
148 """
149 try:
150 self.throw(GeneratorExit)
151 except (GeneratorExit, StopIteration):
152 pass
153 else:
154 raise RuntimeError("coroutine ignored GeneratorExit")
155
Yury Selivanov75445082015-05-11 22:57:16 -0400156 @classmethod
157 def __subclasshook__(cls, C):
Yury Selivanov56fc6142015-05-29 09:01:29 -0400158 if cls is Coroutine:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700159 return _check_methods(C, '__await__', 'send', 'throw', 'close')
Yury Selivanov75445082015-05-11 22:57:16 -0400160 return NotImplemented
161
Yury Selivanov75445082015-05-11 22:57:16 -0400162
Yury Selivanov5376ba92015-06-22 12:19:30 -0400163Coroutine.register(coroutine)
164
165
Yury Selivanove0104ae2015-05-14 12:19:16 -0400166class AsyncIterable(metaclass=ABCMeta):
167
168 __slots__ = ()
169
170 @abstractmethod
Yury Selivanova6f6edb2016-06-09 15:08:31 -0400171 def __aiter__(self):
Yury Selivanove0104ae2015-05-14 12:19:16 -0400172 return AsyncIterator()
173
174 @classmethod
175 def __subclasshook__(cls, C):
176 if cls is AsyncIterable:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700177 return _check_methods(C, "__aiter__")
Yury Selivanove0104ae2015-05-14 12:19:16 -0400178 return NotImplemented
179
Guido van Rossum48b069a2020-04-07 09:50:06 -0700180 __class_getitem__ = classmethod(GenericAlias)
181
Yury Selivanove0104ae2015-05-14 12:19:16 -0400182
183class AsyncIterator(AsyncIterable):
184
185 __slots__ = ()
186
187 @abstractmethod
188 async def __anext__(self):
189 """Return the next item or raise StopAsyncIteration when exhausted."""
190 raise StopAsyncIteration
191
Yury Selivanova6f6edb2016-06-09 15:08:31 -0400192 def __aiter__(self):
Yury Selivanove0104ae2015-05-14 12:19:16 -0400193 return self
194
195 @classmethod
196 def __subclasshook__(cls, C):
197 if cls is AsyncIterator:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700198 return _check_methods(C, "__anext__", "__aiter__")
Yury Selivanove0104ae2015-05-14 12:19:16 -0400199 return NotImplemented
200
201
Yury Selivanov22214ab2016-11-16 18:25:04 -0500202class AsyncGenerator(AsyncIterator):
203
204 __slots__ = ()
205
206 async def __anext__(self):
207 """Return the next item from the asynchronous generator.
208 When exhausted, raise StopAsyncIteration.
209 """
210 return await self.asend(None)
211
212 @abstractmethod
213 async def asend(self, value):
214 """Send a value into the asynchronous generator.
215 Return next yielded value or raise StopAsyncIteration.
216 """
217 raise StopAsyncIteration
218
219 @abstractmethod
220 async def athrow(self, typ, val=None, tb=None):
221 """Raise an exception in the asynchronous generator.
222 Return next yielded value or raise StopAsyncIteration.
223 """
224 if val is None:
225 if tb is None:
226 raise typ
227 val = typ()
228 if tb is not None:
229 val = val.with_traceback(tb)
230 raise val
231
232 async def aclose(self):
233 """Raise GeneratorExit inside coroutine.
234 """
235 try:
236 await self.athrow(GeneratorExit)
237 except (GeneratorExit, StopAsyncIteration):
238 pass
239 else:
240 raise RuntimeError("asynchronous generator ignored GeneratorExit")
241
242 @classmethod
243 def __subclasshook__(cls, C):
244 if cls is AsyncGenerator:
245 return _check_methods(C, '__aiter__', '__anext__',
246 'asend', 'athrow', 'aclose')
247 return NotImplemented
248
249
250AsyncGenerator.register(async_generator)
251
252
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000253class Iterable(metaclass=ABCMeta):
254
Raymond Hettingerc46759a2011-03-22 11:46:25 -0700255 __slots__ = ()
256
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000257 @abstractmethod
258 def __iter__(self):
259 while False:
260 yield None
261
262 @classmethod
263 def __subclasshook__(cls, C):
264 if cls is Iterable:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700265 return _check_methods(C, "__iter__")
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000266 return NotImplemented
267
Guido van Rossum48b069a2020-04-07 09:50:06 -0700268 __class_getitem__ = classmethod(GenericAlias)
269
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000270
Raymond Hettinger74b64952008-02-09 02:53:48 +0000271class Iterator(Iterable):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000272
Raymond Hettingerc46759a2011-03-22 11:46:25 -0700273 __slots__ = ()
274
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000275 @abstractmethod
276 def __next__(self):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700277 'Return the next item from the iterator. When exhausted, raise StopIteration'
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000278 raise StopIteration
279
280 def __iter__(self):
281 return self
282
283 @classmethod
284 def __subclasshook__(cls, C):
285 if cls is Iterator:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700286 return _check_methods(C, '__iter__', '__next__')
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000287 return NotImplemented
288
Guido van Rossum48b069a2020-04-07 09:50:06 -0700289
Christian Heimesf83be4e2007-11-28 09:44:38 +0000290Iterator.register(bytes_iterator)
291Iterator.register(bytearray_iterator)
292#Iterator.register(callable_iterator)
293Iterator.register(dict_keyiterator)
294Iterator.register(dict_valueiterator)
295Iterator.register(dict_itemiterator)
296Iterator.register(list_iterator)
297Iterator.register(list_reverseiterator)
298Iterator.register(range_iterator)
Serhiy Storchaka48b1c3f2016-10-08 22:04:12 +0300299Iterator.register(longrange_iterator)
Christian Heimesf83be4e2007-11-28 09:44:38 +0000300Iterator.register(set_iterator)
301Iterator.register(str_iterator)
302Iterator.register(tuple_iterator)
303Iterator.register(zip_iterator)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000304
Raymond Hettingerbd60e8d2015-05-09 01:07:23 -0400305
Guido van Rossum16ca06b2016-04-04 10:59:29 -0700306class Reversible(Iterable):
307
308 __slots__ = ()
309
310 @abstractmethod
311 def __reversed__(self):
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700312 while False:
313 yield None
Guido van Rossum16ca06b2016-04-04 10:59:29 -0700314
315 @classmethod
316 def __subclasshook__(cls, C):
317 if cls is Reversible:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700318 return _check_methods(C, "__reversed__", "__iter__")
Guido van Rossum16ca06b2016-04-04 10:59:29 -0700319 return NotImplemented
320
321
Raymond Hettingerbd60e8d2015-05-09 01:07:23 -0400322class Generator(Iterator):
323
324 __slots__ = ()
325
326 def __next__(self):
327 """Return the next item from the generator.
328 When exhausted, raise StopIteration.
329 """
330 return self.send(None)
331
332 @abstractmethod
333 def send(self, value):
334 """Send a value into the generator.
335 Return next yielded value or raise StopIteration.
336 """
337 raise StopIteration
338
339 @abstractmethod
340 def throw(self, typ, val=None, tb=None):
341 """Raise an exception in the generator.
342 Return next yielded value or raise StopIteration.
343 """
344 if val is None:
345 if tb is None:
346 raise typ
347 val = typ()
348 if tb is not None:
349 val = val.with_traceback(tb)
350 raise val
351
352 def close(self):
353 """Raise GeneratorExit inside generator.
354 """
355 try:
356 self.throw(GeneratorExit)
357 except (GeneratorExit, StopIteration):
358 pass
359 else:
360 raise RuntimeError("generator ignored GeneratorExit")
361
362 @classmethod
363 def __subclasshook__(cls, C):
364 if cls is Generator:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700365 return _check_methods(C, '__iter__', '__next__',
366 'send', 'throw', 'close')
Raymond Hettingerbd60e8d2015-05-09 01:07:23 -0400367 return NotImplemented
368
Guido van Rossum48b069a2020-04-07 09:50:06 -0700369
Raymond Hettingerbd60e8d2015-05-09 01:07:23 -0400370Generator.register(generator)
371
372
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000373class Sized(metaclass=ABCMeta):
374
Raymond Hettingerc46759a2011-03-22 11:46:25 -0700375 __slots__ = ()
376
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000377 @abstractmethod
378 def __len__(self):
379 return 0
380
381 @classmethod
382 def __subclasshook__(cls, C):
383 if cls is Sized:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700384 return _check_methods(C, "__len__")
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000385 return NotImplemented
386
387
388class Container(metaclass=ABCMeta):
389
Raymond Hettingerc46759a2011-03-22 11:46:25 -0700390 __slots__ = ()
391
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000392 @abstractmethod
393 def __contains__(self, x):
394 return False
395
396 @classmethod
397 def __subclasshook__(cls, C):
398 if cls is Container:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700399 return _check_methods(C, "__contains__")
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000400 return NotImplemented
401
Guido van Rossum48b069a2020-04-07 09:50:06 -0700402 __class_getitem__ = classmethod(GenericAlias)
403
404
Guido van Rossumf0666942016-08-23 10:47:07 -0700405class Collection(Sized, Iterable, Container):
406
407 __slots__ = ()
408
409 @classmethod
410 def __subclasshook__(cls, C):
411 if cls is Collection:
412 return _check_methods(C, "__len__", "__iter__", "__contains__")
413 return NotImplemented
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000414
Guido van Rossum48b069a2020-04-07 09:50:06 -0700415
kj463c7d32020-12-14 02:38:24 +0800416class _CallableGenericAlias(GenericAlias):
417 """ Represent `Callable[argtypes, resulttype]`.
418
419 This sets ``__args__`` to a tuple containing the flattened``argtypes``
420 followed by ``resulttype``.
421
422 Example: ``Callable[[int, str], float]`` sets ``__args__`` to
423 ``(int, str, float)``.
424 """
425
426 __slots__ = ()
427
428 def __new__(cls, origin, args):
429 return cls.__create_ga(origin, args)
430
431 @classmethod
432 def __create_ga(cls, origin, args):
433 if not isinstance(args, tuple) or len(args) != 2:
434 raise TypeError(
435 "Callable must be used as Callable[[arg, ...], result].")
436 t_args, t_result = args
kj6dd3da32020-12-24 10:47:40 +0800437 if isinstance(t_args, (list, tuple)):
kj463c7d32020-12-14 02:38:24 +0800438 ga_args = tuple(t_args) + (t_result,)
439 # This relaxes what t_args can be on purpose to allow things like
440 # PEP 612 ParamSpec. Responsibility for whether a user is using
441 # Callable[...] properly is deferred to static type checkers.
442 else:
443 ga_args = args
444 return super().__new__(cls, origin, ga_args)
445
446 def __repr__(self):
447 if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
448 return super().__repr__()
449 return (f'collections.abc.Callable'
450 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
451 f'{_type_repr(self.__args__[-1])}]')
452
453 def __reduce__(self):
454 args = self.__args__
455 if not (len(args) == 2 and args[0] is Ellipsis):
456 args = list(args[:-1]), args[-1]
457 return _CallableGenericAlias, (Callable, args)
458
kj6dd3da32020-12-24 10:47:40 +0800459 def __getitem__(self, item):
460 # Called during TypeVar substitution, returns the custom subclass
461 # rather than the default types.GenericAlias object.
462 ga = super().__getitem__(item)
463 args = ga.__args__
464 t_result = args[-1]
465 t_args = args[:-1]
466 args = (t_args, t_result)
467 return _CallableGenericAlias(Callable, args)
468
kj463c7d32020-12-14 02:38:24 +0800469
470def _type_repr(obj):
471 """Return the repr() of an object, special-casing types (internal helper).
472
473 Copied from :mod:`typing` since collections.abc
474 shouldn't depend on that module.
475 """
476 if isinstance(obj, GenericAlias):
477 return repr(obj)
478 if isinstance(obj, type):
479 if obj.__module__ == 'builtins':
480 return obj.__qualname__
481 return f'{obj.__module__}.{obj.__qualname__}'
482 if obj is Ellipsis:
483 return '...'
484 if isinstance(obj, FunctionType):
485 return obj.__name__
486 return repr(obj)
487
488
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000489class Callable(metaclass=ABCMeta):
490
Raymond Hettingerc46759a2011-03-22 11:46:25 -0700491 __slots__ = ()
492
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000493 @abstractmethod
Christian Heimes78644762008-03-04 23:39:23 +0000494 def __call__(self, *args, **kwds):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000495 return False
496
497 @classmethod
498 def __subclasshook__(cls, C):
499 if cls is Callable:
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700500 return _check_methods(C, "__call__")
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000501 return NotImplemented
502
kj463c7d32020-12-14 02:38:24 +0800503 __class_getitem__ = classmethod(_CallableGenericAlias)
Guido van Rossum48b069a2020-04-07 09:50:06 -0700504
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000505
506### SETS ###
507
508
Guido van Rossumf0666942016-08-23 10:47:07 -0700509class Set(Collection):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000510 """A set is a finite, iterable container.
511
512 This class provides concrete generic implementations of all
513 methods except for __contains__, __iter__ and __len__.
514
515 To override the comparisons (presumably for speed, as the
Raymond Hettinger11cda472014-07-03 00:31:30 +0100516 semantics are fixed), redefine __le__ and __ge__,
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000517 then the other operations will automatically follow suit.
518 """
519
Raymond Hettingerc46759a2011-03-22 11:46:25 -0700520 __slots__ = ()
521
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000522 def __le__(self, other):
523 if not isinstance(other, Set):
524 return NotImplemented
525 if len(self) > len(other):
526 return False
527 for elem in self:
528 if elem not in other:
529 return False
530 return True
531
532 def __lt__(self, other):
533 if not isinstance(other, Set):
534 return NotImplemented
535 return len(self) < len(other) and self.__le__(other)
536
Raymond Hettinger71909422008-02-09 00:08:16 +0000537 def __gt__(self, other):
538 if not isinstance(other, Set):
539 return NotImplemented
Raymond Hettingerdd5e53a2014-05-26 00:09:04 -0700540 return len(self) > len(other) and self.__ge__(other)
Raymond Hettinger71909422008-02-09 00:08:16 +0000541
542 def __ge__(self, other):
543 if not isinstance(other, Set):
544 return NotImplemented
Raymond Hettingerdd5e53a2014-05-26 00:09:04 -0700545 if len(self) < len(other):
546 return False
547 for elem in other:
548 if elem not in self:
549 return False
550 return True
Raymond Hettinger71909422008-02-09 00:08:16 +0000551
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000552 def __eq__(self, other):
553 if not isinstance(other, Set):
554 return NotImplemented
555 return len(self) == len(other) and self.__le__(other)
556
557 @classmethod
558 def _from_iterable(cls, it):
Raymond Hettinger8284c4a2008-02-06 20:47:09 +0000559 '''Construct an instance of the class from any iterable input.
560
561 Must override this method if the class constructor signature
Raymond Hettinger7aebb642008-02-09 03:25:08 +0000562 does not accept an iterable for an input.
Raymond Hettinger8284c4a2008-02-06 20:47:09 +0000563 '''
Raymond Hettinger7aebb642008-02-09 03:25:08 +0000564 return cls(it)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000565
566 def __and__(self, other):
567 if not isinstance(other, Iterable):
568 return NotImplemented
569 return self._from_iterable(value for value in other if value in self)
570
Raymond Hettingerdd5e53a2014-05-26 00:09:04 -0700571 __rand__ = __and__
572
Christian Heimes190d79e2008-01-30 11:58:22 +0000573 def isdisjoint(self, other):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700574 'Return True if two sets have a null intersection.'
Christian Heimes190d79e2008-01-30 11:58:22 +0000575 for value in other:
576 if value in self:
577 return False
578 return True
579
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000580 def __or__(self, other):
581 if not isinstance(other, Iterable):
582 return NotImplemented
Christian Heimes78644762008-03-04 23:39:23 +0000583 chain = (e for s in (self, other) for e in s)
584 return self._from_iterable(chain)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000585
Raymond Hettingerdd5e53a2014-05-26 00:09:04 -0700586 __ror__ = __or__
587
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000588 def __sub__(self, other):
589 if not isinstance(other, Set):
590 if not isinstance(other, Iterable):
591 return NotImplemented
592 other = self._from_iterable(other)
593 return self._from_iterable(value for value in self
594 if value not in other)
595
Raymond Hettingerdd5e53a2014-05-26 00:09:04 -0700596 def __rsub__(self, other):
597 if not isinstance(other, Set):
598 if not isinstance(other, Iterable):
599 return NotImplemented
600 other = self._from_iterable(other)
601 return self._from_iterable(value for value in other
602 if value not in self)
603
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000604 def __xor__(self, other):
605 if not isinstance(other, Set):
606 if not isinstance(other, Iterable):
607 return NotImplemented
608 other = self._from_iterable(other)
609 return (self - other) | (other - self)
610
Raymond Hettingerdd5e53a2014-05-26 00:09:04 -0700611 __rxor__ = __xor__
612
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000613 def _hash(self):
614 """Compute the hash value of a set.
615
616 Note that we don't define __hash__: not all sets are hashable.
617 But if you define a hashable set type, its __hash__ should
618 call this function.
619
620 This must be compatible __eq__.
621
622 All sets ought to compare equal if they contain the same
623 elements, regardless of how they are implemented, and
624 regardless of the order of the elements; so there's not much
625 freedom for __eq__ or __hash__. We match the algorithm used
626 by the built-in frozenset type.
627 """
Christian Heimesa37d4c62007-12-04 23:02:19 +0000628 MAX = sys.maxsize
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000629 MASK = 2 * MAX + 1
630 n = len(self)
631 h = 1927868237 * (n + 1)
632 h &= MASK
633 for x in self:
634 hx = hash(x)
635 h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
636 h &= MASK
637 h = h * 69069 + 907133923
638 h &= MASK
639 if h > MAX:
640 h -= MASK + 1
641 if h == -1:
642 h = 590923713
643 return h
644
Guido van Rossum48b069a2020-04-07 09:50:06 -0700645
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000646Set.register(frozenset)
647
648
649class MutableSet(Set):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700650 """A mutable set is a finite, iterable container.
651
652 This class provides concrete generic implementations of all
653 methods except for __contains__, __iter__, __len__,
654 add(), and discard().
655
656 To override the comparisons (presumably for speed, as the
657 semantics are fixed), all you have to do is redefine __le__ and
658 then the other operations will automatically follow suit.
659 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000660
Raymond Hettingerc46759a2011-03-22 11:46:25 -0700661 __slots__ = ()
662
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000663 @abstractmethod
664 def add(self, value):
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000665 """Add an element."""
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000666 raise NotImplementedError
667
668 @abstractmethod
669 def discard(self, value):
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000670 """Remove an element. Do not raise an exception if absent."""
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000671 raise NotImplementedError
672
Christian Heimes190d79e2008-01-30 11:58:22 +0000673 def remove(self, value):
674 """Remove an element. If not a member, raise a KeyError."""
675 if value not in self:
676 raise KeyError(value)
677 self.discard(value)
678
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000679 def pop(self):
680 """Return the popped value. Raise KeyError if empty."""
681 it = iter(self)
682 try:
Raymond Hettingerae650182009-01-28 23:33:59 +0000683 value = next(it)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000684 except StopIteration:
Serhiy Storchaka5affd232017-04-05 09:37:24 +0300685 raise KeyError from None
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000686 self.discard(value)
687 return value
688
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000689 def clear(self):
690 """This is slow (creates N new iterators!) but effective."""
691 try:
692 while True:
693 self.pop()
694 except KeyError:
695 pass
696
Raymond Hettingerb3d89a42011-01-12 20:37:47 +0000697 def __ior__(self, it):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000698 for value in it:
699 self.add(value)
700 return self
701
Raymond Hettingerb3d89a42011-01-12 20:37:47 +0000702 def __iand__(self, it):
Raymond Hettinger3f10a952009-04-01 19:05:50 +0000703 for value in (self - it):
704 self.discard(value)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000705 return self
706
Raymond Hettingerb3d89a42011-01-12 20:37:47 +0000707 def __ixor__(self, it):
Daniel Stutzbach31da5b22010-08-24 20:49:57 +0000708 if it is self:
709 self.clear()
710 else:
711 if not isinstance(it, Set):
712 it = self._from_iterable(it)
713 for value in it:
714 if value in self:
715 self.discard(value)
716 else:
717 self.add(value)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000718 return self
719
Raymond Hettingerb3d89a42011-01-12 20:37:47 +0000720 def __isub__(self, it):
Daniel Stutzbach31da5b22010-08-24 20:49:57 +0000721 if it is self:
722 self.clear()
723 else:
724 for value in it:
725 self.discard(value)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000726 return self
727
Guido van Rossum48b069a2020-04-07 09:50:06 -0700728
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000729MutableSet.register(set)
730
731
732### MAPPINGS ###
733
734
Guido van Rossumf0666942016-08-23 10:47:07 -0700735class Mapping(Collection):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700736 """A Mapping is a generic container for associating key/value
737 pairs.
738
739 This class provides concrete generic implementations of all
740 methods except for __getitem__, __iter__, and __len__.
Raymond Hettinger153866e2013-03-24 15:20:29 -0700741 """
742
Julien Palard282282a2020-11-17 22:50:23 +0100743 __slots__ = ()
744
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000745 @abstractmethod
746 def __getitem__(self, key):
747 raise KeyError
748
749 def get(self, key, default=None):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700750 'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000751 try:
752 return self[key]
753 except KeyError:
754 return default
755
756 def __contains__(self, key):
757 try:
758 self[key]
759 except KeyError:
760 return False
761 else:
762 return True
763
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000764 def keys(self):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700765 "D.keys() -> a set-like object providing a view on D's keys"
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000766 return KeysView(self)
767
768 def items(self):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700769 "D.items() -> a set-like object providing a view on D's items"
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000770 return ItemsView(self)
771
772 def values(self):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700773 "D.values() -> an object providing a view on D's values"
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000774 return ValuesView(self)
775
Raymond Hettingerb9da9bc2008-02-04 20:44:31 +0000776 def __eq__(self, other):
Benjamin Peterson4ad6bd52010-05-21 20:55:22 +0000777 if not isinstance(other, Mapping):
778 return NotImplemented
779 return dict(self.items()) == dict(other.items())
Raymond Hettingerb9da9bc2008-02-04 20:44:31 +0000780
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700781 __reversed__ = None
782
Guido van Rossum48b069a2020-04-07 09:50:06 -0700783
Victor Stinner7b17a4e2012-04-20 01:41:36 +0200784Mapping.register(mappingproxy)
785
Christian Heimes2202f872008-02-06 14:31:34 +0000786
Raymond Hettingerbfd06122008-02-09 10:04:32 +0000787class MappingView(Sized):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000788
Raymond Hettinger3170d1c2014-05-03 19:06:32 -0700789 __slots__ = '_mapping',
790
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000791 def __init__(self, mapping):
792 self._mapping = mapping
793
794 def __len__(self):
795 return len(self._mapping)
796
Raymond Hettinger89fc2b72009-02-27 07:47:32 +0000797 def __repr__(self):
798 return '{0.__class__.__name__}({0._mapping!r})'.format(self)
799
Guido van Rossum48b069a2020-04-07 09:50:06 -0700800 __class_getitem__ = classmethod(GenericAlias)
801
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000802
803class KeysView(MappingView, Set):
804
Raymond Hettinger3170d1c2014-05-03 19:06:32 -0700805 __slots__ = ()
806
Raymond Hettinger9117c752010-08-22 07:44:24 +0000807 @classmethod
808 def _from_iterable(self, it):
809 return set(it)
810
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000811 def __contains__(self, key):
812 return key in self._mapping
813
814 def __iter__(self):
Philip Jenvey4993cc02012-10-01 12:53:43 -0700815 yield from self._mapping
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000816
Guido van Rossum48b069a2020-04-07 09:50:06 -0700817
Christian Heimesf83be4e2007-11-28 09:44:38 +0000818KeysView.register(dict_keys)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000819
820
821class ItemsView(MappingView, Set):
822
Raymond Hettinger3170d1c2014-05-03 19:06:32 -0700823 __slots__ = ()
824
Raymond Hettinger9117c752010-08-22 07:44:24 +0000825 @classmethod
826 def _from_iterable(self, it):
827 return set(it)
828
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000829 def __contains__(self, item):
830 key, value = item
831 try:
832 v = self._mapping[key]
833 except KeyError:
834 return False
835 else:
Raymond Hettinger584e8ae2016-05-05 11:14:06 +0300836 return v is value or v == value
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000837
838 def __iter__(self):
839 for key in self._mapping:
840 yield (key, self._mapping[key])
841
Guido van Rossum48b069a2020-04-07 09:50:06 -0700842
Christian Heimesf83be4e2007-11-28 09:44:38 +0000843ItemsView.register(dict_items)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000844
845
Raymond Hettinger02556fb2018-01-11 21:53:49 -0800846class ValuesView(MappingView, Collection):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000847
Raymond Hettinger3170d1c2014-05-03 19:06:32 -0700848 __slots__ = ()
849
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000850 def __contains__(self, value):
851 for key in self._mapping:
Raymond Hettinger584e8ae2016-05-05 11:14:06 +0300852 v = self._mapping[key]
853 if v is value or v == value:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000854 return True
855 return False
856
857 def __iter__(self):
858 for key in self._mapping:
859 yield self._mapping[key]
860
Guido van Rossum48b069a2020-04-07 09:50:06 -0700861
Christian Heimesf83be4e2007-11-28 09:44:38 +0000862ValuesView.register(dict_values)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000863
864
865class MutableMapping(Mapping):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700866 """A MutableMapping is a generic container for associating
867 key/value pairs.
868
869 This class provides concrete generic implementations of all
870 methods except for __getitem__, __setitem__, __delitem__,
871 __iter__, and __len__.
Raymond Hettinger153866e2013-03-24 15:20:29 -0700872 """
873
Julien Palard282282a2020-11-17 22:50:23 +0100874 __slots__ = ()
875
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000876 @abstractmethod
877 def __setitem__(self, key, value):
878 raise KeyError
879
880 @abstractmethod
881 def __delitem__(self, key):
882 raise KeyError
883
884 __marker = object()
885
886 def pop(self, key, default=__marker):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700887 '''D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
888 If key is not found, d is returned if given, otherwise KeyError is raised.
889 '''
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000890 try:
891 value = self[key]
892 except KeyError:
893 if default is self.__marker:
894 raise
895 return default
896 else:
897 del self[key]
898 return value
899
900 def popitem(self):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700901 '''D.popitem() -> (k, v), remove and return some (key, value) pair
902 as a 2-tuple; but raise KeyError if D is empty.
903 '''
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000904 try:
905 key = next(iter(self))
906 except StopIteration:
Serhiy Storchaka5affd232017-04-05 09:37:24 +0300907 raise KeyError from None
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000908 value = self[key]
909 del self[key]
910 return key, value
911
912 def clear(self):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700913 'D.clear() -> None. Remove all items from D.'
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000914 try:
915 while True:
916 self.popitem()
917 except KeyError:
918 pass
919
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300920 def update(self, other=(), /, **kwds):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700921 ''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
922 If E present and has a .keys() method, does: for k in E: D[k] = E[k]
923 If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
924 In either case, this is followed by: for k, v in F.items(): D[k] = v
925 '''
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300926 if isinstance(other, Mapping):
927 for key in other:
928 self[key] = other[key]
929 elif hasattr(other, "keys"):
930 for key in other.keys():
931 self[key] = other[key]
932 else:
933 for key, value in other:
934 self[key] = value
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000935 for key, value in kwds.items():
936 self[key] = value
937
Raymond Hettingerb9da9bc2008-02-04 20:44:31 +0000938 def setdefault(self, key, default=None):
Raymond Hettinger153866e2013-03-24 15:20:29 -0700939 'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'
Raymond Hettingerb9da9bc2008-02-04 20:44:31 +0000940 try:
941 return self[key]
942 except KeyError:
943 self[key] = default
944 return default
945
Guido van Rossum48b069a2020-04-07 09:50:06 -0700946
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000947MutableMapping.register(dict)
948
949
950### SEQUENCES ###
951
952
Guido van Rossumf0666942016-08-23 10:47:07 -0700953class Sequence(Reversible, Collection):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000954 """All the operations on a read-only sequence.
955
956 Concrete subclasses must override __new__ or __init__,
957 __getitem__, and __len__.
958 """
959
Raymond Hettingerc46759a2011-03-22 11:46:25 -0700960 __slots__ = ()
961
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000962 @abstractmethod
963 def __getitem__(self, index):
964 raise IndexError
965
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000966 def __iter__(self):
967 i = 0
Raymond Hettinger71909422008-02-09 00:08:16 +0000968 try:
969 while True:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000970 v = self[i]
Raymond Hettinger71909422008-02-09 00:08:16 +0000971 yield v
972 i += 1
973 except IndexError:
974 return
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000975
976 def __contains__(self, value):
977 for v in self:
Raymond Hettinger584e8ae2016-05-05 11:14:06 +0300978 if v is value or v == value:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000979 return True
980 return False
981
982 def __reversed__(self):
983 for i in reversed(range(len(self))):
984 yield self[i]
985
Raymond Hettingerec219ba2015-05-22 19:29:22 -0700986 def index(self, value, start=0, stop=None):
987 '''S.index(value, [start, [stop]]) -> integer -- return first index of value.
Raymond Hettinger153866e2013-03-24 15:20:29 -0700988 Raises ValueError if the value is not present.
Nitish Chandra5ce0a2a2017-12-12 15:52:30 +0530989
990 Supporting start and stop arguments is optional, but
991 recommended.
Raymond Hettinger153866e2013-03-24 15:20:29 -0700992 '''
Raymond Hettingerec219ba2015-05-22 19:29:22 -0700993 if start is not None and start < 0:
994 start = max(len(self) + start, 0)
995 if stop is not None and stop < 0:
996 stop += len(self)
997
998 i = start
999 while stop is None or i < stop:
1000 try:
Xiang Zhangd5d32492017-03-08 11:04:24 +08001001 v = self[i]
1002 if v is value or v == value:
Raymond Hettingerec219ba2015-05-22 19:29:22 -07001003 return i
1004 except IndexError:
1005 break
1006 i += 1
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001007 raise ValueError
1008
1009 def count(self, value):
Raymond Hettinger153866e2013-03-24 15:20:29 -07001010 'S.count(value) -> integer -- return number of occurrences of value'
Xiang Zhangd5d32492017-03-08 11:04:24 +08001011 return sum(1 for v in self if v is value or v == value)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001012
Guido van Rossum48b069a2020-04-07 09:50:06 -07001013
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001014Sequence.register(tuple)
Guido van Rossum3172c5d2007-10-16 18:12:55 +00001015Sequence.register(str)
Raymond Hettinger9aa53c22009-02-24 11:25:35 +00001016Sequence.register(range)
Nick Coghlan45163cc2013-10-02 22:31:47 +10001017Sequence.register(memoryview)
Guido van Rossumd05eb002007-11-21 22:26:24 +00001018
1019
1020class ByteString(Sequence):
Guido van Rossumd05eb002007-11-21 22:26:24 +00001021 """This unifies bytes and bytearray.
1022
1023 XXX Should add all their methods.
1024 """
1025
Raymond Hettingerc46759a2011-03-22 11:46:25 -07001026 __slots__ = ()
1027
Guido van Rossumd05eb002007-11-21 22:26:24 +00001028ByteString.register(bytes)
1029ByteString.register(bytearray)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001030
1031
1032class MutableSequence(Sequence):
Guido van Rossum840c3102013-07-25 11:55:41 -07001033 """All the operations on a read-write sequence.
Raymond Hettinger153866e2013-03-24 15:20:29 -07001034
1035 Concrete subclasses must provide __new__ or __init__,
1036 __getitem__, __setitem__, __delitem__, __len__, and insert().
Raymond Hettinger153866e2013-03-24 15:20:29 -07001037 """
1038
Julien Palard282282a2020-11-17 22:50:23 +01001039 __slots__ = ()
1040
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001041 @abstractmethod
1042 def __setitem__(self, index, value):
1043 raise IndexError
1044
1045 @abstractmethod
1046 def __delitem__(self, index):
1047 raise IndexError
1048
1049 @abstractmethod
1050 def insert(self, index, value):
Raymond Hettinger153866e2013-03-24 15:20:29 -07001051 'S.insert(index, value) -- insert value before index'
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001052 raise IndexError
1053
1054 def append(self, value):
Raymond Hettinger153866e2013-03-24 15:20:29 -07001055 'S.append(value) -- append value to the end of the sequence'
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001056 self.insert(len(self), value)
1057
Eli Bendersky9479d1a2011-03-04 05:34:58 +00001058 def clear(self):
Raymond Hettinger153866e2013-03-24 15:20:29 -07001059 'S.clear() -> None -- remove all items from S'
Eli Bendersky9479d1a2011-03-04 05:34:58 +00001060 try:
1061 while True:
1062 self.pop()
1063 except IndexError:
1064 pass
1065
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001066 def reverse(self):
Raymond Hettinger153866e2013-03-24 15:20:29 -07001067 'S.reverse() -- reverse *IN PLACE*'
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001068 n = len(self)
1069 for i in range(n//2):
1070 self[i], self[n-i-1] = self[n-i-1], self[i]
1071
1072 def extend(self, values):
Raymond Hettinger153866e2013-03-24 15:20:29 -07001073 'S.extend(iterable) -- extend sequence by appending elements from the iterable'
Naris R1b5f9c92018-08-31 02:56:14 +10001074 if values is self:
1075 values = list(values)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001076 for v in values:
1077 self.append(v)
1078
1079 def pop(self, index=-1):
Raymond Hettinger153866e2013-03-24 15:20:29 -07001080 '''S.pop([index]) -> item -- remove and return item at index (default last).
1081 Raise IndexError if list is empty or index is out of range.
1082 '''
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001083 v = self[index]
1084 del self[index]
1085 return v
1086
1087 def remove(self, value):
Raymond Hettinger153866e2013-03-24 15:20:29 -07001088 '''S.remove(value) -- remove first occurrence of value.
1089 Raise ValueError if the value is not present.
1090 '''
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001091 del self[self.index(value)]
1092
1093 def __iadd__(self, values):
1094 self.extend(values)
Raymond Hettingerc384b222009-05-18 15:35:26 +00001095 return self
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001096
Guido van Rossum48b069a2020-04-07 09:50:06 -07001097
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001098MutableSequence.register(list)
Guido van Rossumd05eb002007-11-21 22:26:24 +00001099MutableSequence.register(bytearray) # Multiply inheriting, see ByteString