memoryview(object)

The memoryview() function returns a memory view object for the specified argument.

Parameters

The memoryview() function takes one parameter:

  • obj - the object whose internal data must be exposed. obj must support the buffer protocol (bytes, bytearray).

Return Value

  • The memoryview() function returns a memory view object.

Examples

# Random byte array
random_byte_array = bytearray('ABC', 'utf-8')

mv = memoryview(random_byte_array)

# Access the zero-th index of the memory view
print(mv[0])

# Create bytes from the memory view
print(bytes(mv[0:2]))

# Create a list from the memory view
print(list(mv[0:3]))

# Result:

65
b'AB'
[65, 66, 67]