b = bytearray(1) m = memoryview(b)
Since type (b [0]) is int, I would expect type (m [0]) to also be int. Starting with Python 3.2, this is true. But in Python 2.7, the type is str. I need to be able to pass a memoryview for functions that expect an int array.
In Python 2.7, I can create a wrapper class and override it __getitem__. But is there a trick to tell the memoryview object to return int elements?
__getitem__
No, there is no way to call memoryview __getitem__to return objects int.
memoryview
int
Use ordshould do the trick:
ord
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) >>> b = bytearray(1) >>> m = memoryview(b) >>> m[0] '\x00' >>> ord(m[0]) 0 >>> map(ord, m) [0] >>>
, Python 2 . Python 3 (unicode).