Python - sequences
Overview
Types in terms of structure
- container sequences
- flat sequences
Can hold items of different types, including nested containers. E.g.
list
, tuple
, and collections.deque
.Hold items of one simple type. E.g.
str
, bytes
, and array.array
.A container sequence holds references to the objects it contains which may be of any type.
A flat sequence stores the value of its contents in its own memory space, not as distinct Python objects.
Note
Every Python object in memory has a header with metadata. E.g. the simplest object float
ob_refcnt
: metadata, reference countob_type
: metadata, a pointer to the object's typeob_fval
: value, aC double
holding the value of thefloat
Types in terms of mutability
- mutable sequences
- immutable sequences
E.g.
list
, bytearray
, and collections.deque
.E.g.
tuple
, str
and bytes
.>>> from collections import abc
>>> issubclass(tuple, abc.Sequence)
True
>>> issubclass(list, abc.MutableSequence)
True