Python: memoryview() function
memoryview() function
The memoryview() function is used to get a memory view object from a specified object.
Version:
(Python 3.2.5)
Syntax:
memoryview(obj)
Parameter:
Name | Description | Required / Optional |
---|---|---|
obj | A memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray. | Required |
Return value:
A memory view object of the given object.
Example: Python memoryview() function
#random bytearray
randomByteArray = bytearray('xyz', 'utf-8')
a = memoryview(randomByteArray)
# access memory view's zeroth index
print(a[0])
# create byte from memory view
print(bytes(a[0:4]))
# create list from memory view
print(list(a[0:6]))
Output:
120 b'xyz' [120, 121, 122]
Python Code Editor:
Test your Python skills with w3resource's quiz
Python: Tips of the Day
How do I check if a list is empty?
For example, if passed the following:
a = [] if not a: print("List is empty")
Ref: https://bit.ly/2A4JXx9
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework