w3resource

Python: memoryview() function

memoryview() function

The memoryview() function is used to get a memory view object from a specified object.

Version:

(Python 3.2.5)

Syntax:

memoryview(obj)

Parameter:

Name Description Required /
Optional
obj A memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray. Required

Return value:

A memory view object of the given object.

Example: Python memoryview() function

#random bytearray
randomByteArray = bytearray('xyz', 'utf-8')

a = memoryview(randomByteArray)

# access memory view's zeroth index
print(a[0])

# create byte from memory view
print(bytes(a[0:4]))

# create list from memory view
print(list(a[0:6]))

Output:

120
b'xyz'
[120, 121, 122]

Python Code Editor:

Previous: max()
Next: min()

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python/built-in-function/memoryview.php