October 25, 2024
Chicago 12, Melborne City, USA
python

What kind of sequence are range and bytearray?


In the "Fluent Python" book by Luciano Ramalho (2nd ed.) he defines the concepts of container sequences and flat sequences:

A container sequence holds references to the objects it contains, which may be of any type, while a flat sequence stores the value of its contents in its own memory space, not as distinct Python objects.

Can we say that objects of type range and bytearray are flat sequences (I think that these objects can’t contain references but not sure)?
And is there a simple way to test whether a sequence is flat or not?


I just found this post that gives a quote from the 1st edition of the "Fluent Python" book (I personally only have the 2nd edition):

Flat sequences: str, bytes, bytearray, memoryview, and array.array hold items of one type.

So, it seems that bytearray is a flat sequence. In the 2nd edition (p.22) the author says only that

Flat sequences – Hold items of one simple type. Some examples: str, bytes, and array.array.
Container sequences – Can hold items of different types, including nested containers. Some examples: list, tuple, and collections.deque.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video