Python memoryview() Method

In this tutorial, we will learn about the python memoryview() method and its uses with examples.

Python memoryview() Method

What is Python memoryview() Method

The memoryview() is a built-in python method that returns a memory allocated by the specified object .

All the objects in python require storage space at the runtime and memoryview() method will return how much storage passed object will take.

The syntax of memoryview() is:

memoryview(object)

Python memoryview() Parameters

The memoryview() method takes only one parameter as argument:

  • object – An object that is exposed using bytes or bytearray.

Example 1: How to use memoryview() in python?

x = memoryview(b"Hello")

print(x)

#return the Unicode of the first character
print(x[0])

#return the Unicode of the second character
print(x[1])

Output:

<memory at 0x7f9efbddadc0>
72
101

Rules of memoryview() 

  • Passed object must be whose internal data is to be exposed