Python memoryview() Function

Python 内置函数Python Built-in Functions


Description

memoryview()The function returns a memory view object for the given argument.

The so-called memory view object refers to wrapping data that supports the buffer protocol, allowing Python code to access it without copying the object.

Syntax

memoryview syntax:

memoryview(obj)

Parameter description:

  • obj -- object

Return Value

Return a list of tuples.

Examples

The following examples demonstrate the usage of memoryview:

Python2.x Usage:

>>>v = memoryview('abcefg') >>> v[1] 'b' >>> v[-1] 'g' >>> v[1:4] <memory at 0x77ab28> >>> v[1:4].tobytes() 'bce'

Python3.x Usage:

>>>v = memoryview(bytearray("abcefg", 'utf-8')) >>> print(v[1]) 98 >>> print(v[-1]) 103 >>> print(v[1:4]) <memory at 0x10f543a08> >>> print(v[1:4].tobytes()) b'bce' >>>

Python 内置函数Python Built-in Functions

Other Extensions