Post

Python memoryview() Method

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

Python memoryview() Method

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:

1
2
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?

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
<memory at 0x7f9efbddadc0>
72
101

Rules of memoryview()

  • Passed object must be whose internal data is to be exposed
This post is licensed under CC BY 4.0 by the author.