What is buffer_info() from an array in Python?
Overview
Python is a high-level programming language that provides functionalities for several operations. The array module comes with various methods to manipulate array data structures.
The buffer_info() function is used to get the memory address and the length of the buffer that holds the array’s contents.
Syntax
(address, length) = arr.buffer_info()
Arguments
This function does not take in any arguments.
Return value
buffer_info() returns a tuple, which contains the memory address and the length of the buffer that holds the array’s contents in the form (address, length).
Example code
#import modulefrom array import array#initialize arraysarray_one = array("i", [1, 2, 3])array_two = array("i", [6, 7, 8, 9])array_three = array("f", [6.4, 2.2, 8.7, 9.0, 10.5])array_four = array("f", [12.5, 16.1, 7.3])#print buffer infoprint (array_one)print (array_one.buffer_info())print ("\n")print (array_two)print (array_two.buffer_info())print ("\n")print (array_three)print (array_three.buffer_info())print ("\n")print (array_four)print (array_four.buffer_info())
Explanation
In the above code, the array module is first imported. We then initialize two arrays with the data types integer in lines 5-6, and two arrays of data type float in lines 7-8. Each array is printed along with its buffer information in the form (address, length) in lines 11-24.