Skip to content

Commit 024f753

Browse files
Docs: Add memoryview methods and attributes
Add 8 methods (tobytes, tolist, toreadonly, release, cast, hex, count, index) and 12 attributes (obj, nbytes, readonly, format, itemsize, ndim, shape, strides, suboffsets, contiguous, c_contiguous, f_contiguous). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a903eb7 commit 024f753

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

docs/builtins/memoryview_func.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@ The `memoryview()` function creates memory views of bytes-like objects without c
1212
| `bytes(mv)` conversion | O(n) | O(n) | n = view size; copies data |
1313
| Modification | O(1) | O(1) | Only if underlying buffer is mutable (e.g., bytearray) |
1414

15+
## Methods
16+
17+
| Method | Time | Space | Notes |
18+
|--------|------|-------|-------|
19+
| `tobytes()` | O(n) | O(n) | Convert to bytes object |
20+
| `tolist()` | O(n) | O(n) | Convert to list of elements |
21+
| `toreadonly()` | O(1) | O(1) | Return read-only version of view |
22+
| `release()` | O(1) | O(1) | Release underlying buffer |
23+
| `cast(format)` | O(1) | O(1) | Reinterpret as different type; must be same byte size |
24+
| `hex()` | O(n) | O(n) | Return hex string representation |
25+
| `count(value)` | O(n) | O(1) | Count occurrences of value |
26+
| `index(value)` | O(n) | O(1) | Find first index of value; raises ValueError if not found |
27+
28+
## Attributes
29+
30+
| Attribute | Time | Notes |
31+
|-----------|------|-------|
32+
| `obj` | O(1) | Underlying object the memoryview refers to |
33+
| `nbytes` | O(1) | Total bytes in the view |
34+
| `readonly` | O(1) | Bool indicating if memory is read-only |
35+
| `format` | O(1) | Struct format string (e.g., 'B' for unsigned byte) |
36+
| `itemsize` | O(1) | Size in bytes of each element |
37+
| `ndim` | O(1) | Number of dimensions |
38+
| `shape` | O(1) | Tuple of dimension sizes |
39+
| `strides` | O(1) | Tuple of bytes to step in each dimension |
40+
| `suboffsets` | O(1) | Tuple for PIL-style arrays; None for simple buffers |
41+
| `contiguous` | O(1) | Bool; True if C or Fortran contiguous |
42+
| `c_contiguous` | O(1) | Bool; True if C-order contiguous |
43+
| `f_contiguous` | O(1) | Bool; True if Fortran-order contiguous |
44+
1545
## Basic Usage
1646

1747
### From Bytes

0 commit comments

Comments
 (0)