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.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

memoryview(obj)

⚙️ Parameters

obj RequiredAn object supporting the buffer protocol (e.g. bytes, bytearray).

Returns:A memoryview object providing zero-copy access to the underlying buffer.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

104 b'ell'
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏