memoryview() Memory view
Last updated: 2026-09-22
memoryview() Memory view
memoryview() exposes the buffer interface of a bytes-like object, allowing zero-copy access.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
memoryview(obj)
⚙️ Parameters
| obj Required | An object supporting the buffer protocol (e.g. bytes, bytearray). |
|---|
Returns:A memoryview object providing zero-copy access to the underlying buffer.
💥 Raises
TypeError— Raised when the object does not support the buffer protocol.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
104 b'ell'
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is memoryview for?
AReading/operating on byte data without copying the underlying buffer — ideal for performance-sensitive cases like large files or network packets, avoiding copies on slicing.
QAfter mv = memoryview(b'hello'), what is mv[0]?
AIt is 104, an integer — like bytes, it indexes by byte. mv[1:4] returns a view; use .tobytes() to convert it back to bytes.
QWhat is the difference between memoryview and a normal slice?
Ab'hello'[1:4] creates a new bytes object (copying the data); memoryview(b'hello')[1:4] is a view pointing at the same underlying buffer, with no copy.
QWhat happens when called on an object that does not support the buffer protocol?
AIt raises a TypeError; only objects like bytes, bytearray, and array.array that support the buffer protocol can be used.