Python - sequences

Overview

Types in terms of structure

  • container sequences
  • Can hold items of different types, including nested containers. E.g. list, tuple, and collections.deque.
  • flat sequences
  • 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 count
  • ob_type: metadata, a pointer to the object's type
  • ob_fval: value, a C double holding the value of the float

Types in terms of mutability

  • mutable sequences
  • E.g. list, bytearray, and collections.deque.
  • immutable sequences
  • E.g. tuple, str and bytes.
>>> from collections import abc
>>> issubclass(tuple, abc.Sequence)
True
>>> issubclass(list, abc.MutableSequence)
True

results matching ""

    No results matching ""