DEV Community

Sungiven
Sungiven

Posted on

built-in sequences in python

so imma go thru 2 set of comparisons between sequences in python.

  1. container vs flat sequences
  2. mutable vs immutable sequences

container vs flat seqs

container seqs are the ones that can hold items of different types, incl nested containers. examples: list, tuple, collections.deque

flat seqs hold items of one simple type. examples: str, bytes, array.array.

a container seq holds refs to objs it contains which can be of any type, while a flat seq stores the value of its items in its own memory space, not distinct python objs.

mutable vs immutable seqs

examples for mutable: list, bytearray, array.array, collections.deque

examples for immutable: tuple, str, bytes

mutable sequences inherit all methods from immutable seqs, and additionally implement additional methods. the built-in concrete seq types don't subclass the Sequence and MutableSequence ABCs, but they're virtual subclasses registered with those ABCs. being virtual subclasses, tuple and list pass these tests:

>>> from collections import abc
>>> issubclass(tuple, abc.Sequence)
True
>>> issubclass(list, abc.MutableSequence)
True
Enter fullscreen mode Exit fullscreen mode

the most fundamental seq types is the list which is a mutable container.

Simplified UML class diagram

Top comments (0)