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



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Returns the symmetric difference between two lists, after applying the provided function to each list element of both:

Example:

def tips_symmetric_difference_by(p, q, fn):
  _p, _q = set(map(fn, p)), set(map(fn, q))
  return [item for item in p if fn(item) not in _q] + [item for item in q if fn(item) not in _p]
from math import floor
print(tips_symmetric_difference_by([4.2, 2.4], [4.6, 6.8],floor))

Output:

[2.4, 6.8]

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook