Introduction: Python memoryview() Function
Some Python programs work with large amounts of binary data. This is common when working with files, images, network data, or byte arrays.
Copying this data every time you need it can slow down the program and use more memory. The Python memoryview() Function helps avoid that.
What it is: The memoryview() function is a built-in Python function that creates a memory view object. It lets you access the data stored in another object without making a copy of it.
See the quick example below to understand how it works.
You’ll also see where the Python memoryview() Function is commonly used.
The following sections explain its syntax, parameters, return value and show how it works with practical examples.
💡 Tip: The memoryview() function is mainly used when working with binary data. To explore more built-in functions, visit the
Python Built-in Functions Learning Guide.
Syntax, Parameters, Return Value and Examples: Python memoryview() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python memoryview() Function.
Syntax
memoryview(object)
Parameters
| Parameter | Description |
|---|---|
object |
An object that supports the buffer protocol, such as bytes, bytearray, or array.array. |
Return Value
| Return Value | Description |
|---|---|
memoryview |
Returns a memory view object that provides direct access to the original data without copying it. |
Quick Example
The following example creates a memory view from a byte array.
data = bytearray(b"ABC")
view = memoryview(data)
print(view[0])
# Output:
65
The memoryview() function creates a view of the original bytearray. The value 65 is the ASCII value of the letter A.
How the Python memoryview() Function Works
- The
memoryview()function takes an object that supports the buffer protocol. - It creates a memory view of the original data.
- No copy of the data is created.
- Changes made through the memory view also change the original object if it is mutable.
- It is useful when working with large amounts of binary data.
- If the object does not support the buffer protocol, Python raises a
TypeError.
Examples: Python memoryview() Function
The examples below show different ways to use the Python memoryview() Function.
Example 1: Creating a Memory View
data = bytearray(b"Python")
view = memoryview(data)
print(view)
# Output:
<memory at 0x...>
Explanation: The memoryview() function creates a memory view object for the original bytearray. The data is not copied.
Example 2: Reading Values from a Memory View
data = bytearray(b"ABC")
view = memoryview(data)
print(view[0])
print(view[1])
# Output:
65
66
Explanation: Each element stores the ASCII value of a character. Here, 65 represents A and 66 represents B.
Example 3: Changing the Original Data
data = bytearray(b"ABC")
view = memoryview(data)
view[0] = 90
print(data)
# Output:
bytearray(b'ZBC')
Explanation: The change is made through the memory view, but the original bytearray is updated because both use the same memory.
Example 4: Slicing a Memory View
data = bytearray(b"Python")
view = memoryview(data)
print(view[1:4].tobytes())
# Output:
b'yth'
Explanation: A memory view can be sliced just like many other Python objects. The selected part can be converted back to bytes when needed.
Example 5: Converting a Memory View to Bytes
data = bytearray(b"Hello")
view = memoryview(data)
print(view.tobytes())
# Output:
b'Hello'
Explanation: The tobytes() method creates a bytes object from the current memory view.
Example 6: Checking the Length of a Memory View
data = bytearray(b"Python")
view = memoryview(data)
print(len(view))
# Output:
6
Explanation: The len() function returns the number of items available through the memory view.
Example 7: Handling an Unsupported Object
try:
memoryview("Python")
except TypeError as e:
print(e)
# Output:
memoryview: a bytes-like object is required, not 'str'
Explanation: Strings do not support the buffer protocol. Because of that, Python raises a TypeError. Using a try-except block prevents the program from stopping.
Use Cases: When to use the memoryview() Function
The Python memoryview() Function is useful in situations like these:
- Working with large binary data.
- Reading and updating byte arrays.
- Processing image or audio data.
- Handling network packets.
- Reducing unnecessary data copies.
- Improving memory usage in large programs.
Key Takeaways: memoryview() Function
Here are the main points to remember about the Python memoryview() Function:
- The
memoryview()function creates a memory view object. - It gives direct access to the original data.
- No copy of the data is created.
- Changes made through the memory view also update the original object if it is mutable.
- It works only with objects that support the buffer protocol.
- Passing an unsupported object raises a
TypeError.
The memoryview() function is useful when working with binary data. Since it avoids copying data, it can help improve memory usage and performance in many programs.