Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memoryview for List
#1
Hi Everyone,

I need help on knowing if we can create a memoryview object for "List". I tried doing the basic steps, but this is the error I am getting:
>>> a  = [1, 2, 3]
>>> type(a)
<class 'list'>
>>> memoryview(a)
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: memoryview: a bytes-like object is required, not 'list'
So, is there a way to create memoryview object for list datatype? Or it is not possible at all?

Thanks for help.
Reply
#2
>>> from array import array
>>> a = array('h', [1,2,3])
>>> mview = memoryview(a)
>>> mview.tolist()
[1, 2, 3]
Reply
#3
But I need to convert list to memoryview object, and not a memoryview object to list
Reply
#4
Look at the code closer.
I convert to memory view and then back again
mview is the memoryview.
Reply
#5
Ok. Now I got your point. Thanks for this suggestion.
So, it basically means, that only array types support buffer protocol and not lists?
Reply
#6
Good practice is to make sure you understand what memoryview is. Best place to start is Python official documentation (class memoryview(obj))

Quote:Create a memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray.

So we need bytes or bytarray (or have our own class).

Maybe following will give general idea:

>>> m = memoryview(b'[1, 2, 3]')
>>> list(m)
[91, 49, 44, 32, 50, 44, 32, 51, 93]
>>> for el in m:
...     print(chr(el))
...
[
1
,
 
2
,
 
3
]
>>> type(m)
memoryview
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
I'm pretty sure that is the case, although I haven't found anything yet that states this definitively.
I haven't found any examples of straight list conversion
Reply
#8
@perfringo, thanks for this aspect. I am able to understand it in a better way.
Reply
#9
Would like to know one more thing, I wanted to try the same behavior for complex number list.
Eg -
>>> a = list([1+1j,2+2j])

But we can't represent the complex number to byte/bytearray, since these are not integers as per my knowledge.

Can you provide more inputs on this?
Reply
#10
First of all - why memoryview? From documentation:

Quote:memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.

'withoug copying' means that main advantage of memoryview is with large binary data. Instead of copying one can have memoryview object.

One additional tidbit from documentation about memoryview and release:

Quote:Many objects take special actions when a view is held on them (for example, a bytearray would temporarily forbid resizing); therefore, calling release() is handy to remove these restrictions (and free any dangling resources) as soon as possible.

Regarding complex numbers - what do you want to accomplish? Bytes and bytearray objects contain single bytes (bytes is immutable and bytearray is a mutable sequence)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python 3 memoryview : unsupported format <d evason 0 2,978 Nov-21-2018, 04:27 PM
Last Post: evason

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020