Python memoryview() Function with Examples

In the previous article, we have discussed Python Program for ord() Function
The memoryview() function in Python returns memory views objects. Before delving deeper into the memoryview() function, consider why we use it.

Why are we using the memoryview() function?
Because Memory view is a safe way to expose the buffer protocol in Python and a memoryview behaves like bytes in many useful contexts (for example, it supports the mapping protocol), it can serve as an adequate replacement if used correctly. The best part is that it uses the buffer protocol under the hood to avoid copies and simply juggle pointers to data. So, before we get into what memory views are available, we must first understand the Buffer Protocol.

What is buffer protocol?

The buffer protocol allows you to access an object’s internal data. This internal data is in the form of a memory array or a buffer. It enables one object to expose its internal data (buffers) and another to access those buffers without requiring intermediate copying. The buffer protocol is only available to us at the C-API level, not through our normal codebase. Memory views are used to expose the same protocol to a standard Python codebase.

Memoryview

Memoryview objects enable Python code to access the internal data of an object that supports the buffer protocol without the need for copying. The memoryview() function provides direct read and write access to byte-oriented data in an object without the need to copy it first. Because it does not create a copy when slicing, this can result in significant performance gains when working with large objects.

Syntax:

memoryview(object)

Parameters

object: The object whose internal data will be exposed.
buffer protocol support – str and bytearray (but not Unicode).

Return Value:

A memoryview object is returned.

Examples:

Example1:

Input:

Given String = 'EFG'

Output:

69
b'EFG'
[69, 70, 71]

Example2:

Input:

Given String = 'uvw'

Output:

117
b'uvw'
[117, 118, 119]

Program for memoryview() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Take a random string and pass it to the bytearray() function with the second argument as ‘utf-8’ and store it in a variable.
  • Calculate the memoryview of the above bytearray by passing the above bytearray to memoryview () function.
  • Store it in another variable.
  • Print or access the zeroth index of the memoryview using the memoryview[0].
  • Create the byte from the memoryview by passing memoryview with slicing to bytes() function and print it.
  • Create a list from the memoryview by passing the memoryview with slicing and convert it into list using the list() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Take a random string and pass it to the bytearray() function with the
# second argument as 'utf-8' and store it in a variable.
randmbyte_arry = bytearray('EFG', 'utf-8')
# Calculate the memoryview of the above bytearray by passing the above
# bytearray to memoryview () function.
# store it in another variable.
memryvieww = memoryview(randmbyte_arry)
# Print or access the zeroth index of the memoryview using the memoryview[0].
print(memryvieww[0])
# Create the byte from the memoryview by passing memoryview with slicing to
# bytes() function and print it.
print(bytes(memryvieww[0:3]))
# Create a list from the memoryview by passing the memoryview with slicing
# and convert it into list using the list() function and print it.
print(list(memryvieww[0:5]))

Output:

69
b'EFG'
[69, 70, 71]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take the above random string and pass it to the bytearray() function with the second argument as ‘utf-8’ and store it in a variable.
  • Calculate the memoryview of the above bytearray by passing the above bytearray to memoryview () function.
  • Store it in another variable.
  • Print or access the zeroth index of the memoryview using the memoryview[0].
  • Create the byte from the memoryview by passing memoryview with slicing to bytes() function and print it.
  • Create a list from the memoryview by passing the memoryview with slicing and convert it into list using the list() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Take the above random string and pass it to the bytearray() function with the
# second argument as 'utf-8' and store it in a variable.
randmbyte_arry = bytearray(gvn_str, 'utf-8')
# Calculate the memoryview of the above bytearray by passing the above
# bytearray to memoryview () function.
# store it in another variable.
memryvieww = memoryview(randmbyte_arry)
# Print or access the zeroth index of the memoryview using the memoryview[0].
print(memryvieww[0])
# Create the byte from the memoryview by passing memoryview with slicing to
# bytes() function and print it.
print(bytes(memryvieww[0:3]))
# Create a list from the memoryview by passing the memoryview with slicing
# and convert it into list using the list() function and print it.
print(list(memryvieww[0:5]))

Output:

Enter some random string = uvw
117
b'uvw'
[117, 118, 119]

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.