[Python-ideas] Memoryview tolist() method is misleading

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Sep 3 17:21:50 CEST 2012


Consider this:

>>> memoryview(b'x').cast('B', ()).tolist()
120

The return value of to list() is an int, not a list.

I suggest to deprecate memoryview.tolist() and .tobytes() methods
(soft deprecation - in documentation only) and recommend using list(m)
and  bytes(m) instead.

For the multidimensional (and 0-dimensional) views, I suggest adding
an unpack([depth]) method that would unpack a view into a nested list
of tuples or subviews.  For example a single-byte scalar should unpack
as follows:

>>> m = memoryview(b'x').cast('B', ())
>>> m.unpack()
(120,)

consistent with

>>> struct.unpack_from(m.format, m)
(120,)



More information about the Python-ideas mailing list