# MemoryView

MemoryView enable slicing action in `bytearray` and `str` WILL NOT return a new object.

Given `bytearray` is mutable and `str`is immutable, only bytearray can update the value.

```python
>> a = 'aaaaaa'
>> b = a[:2]    # Create a new string object

>> a = bytearray('aaaaaa')
>> b = a[:2]    # Create a new bytearray object
>> b[:2] = 'bb' # The modification on b does not affect a
>> a
bytearray(b'aaaaaa')
>> b
bytearray(b'bb')
```

Applying MemoryView

```python
>> a = 'aaaaaa'
>> ma = memoryview(a)
>> ma.readonly  # Read-only MemoryView
True
>> mb = ma[:2]  # Will not create a new String object

>> a = bytearray('aaaaaa')
>> ma = memoryview(a)
>> ma.readonly  # Writable MemoryView
False
>> mb = ma[:2]      # Will not create a new bytearray
>> mb[:2] = 'bb'    # Modification on mb will not affect ma
>> mb.tobytes()
'bb'
>> ma.tobytes()
'bbaaaa'
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://python.books.c-k.dev/skills-and-concept/memoryview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
