Commit 9359ab3f authored by Stefan Behnel's avatar Stefan Behnel

updated array.array section in memoryview docs

parent 5a80280d
...@@ -544,20 +544,24 @@ the buffer interface natively, so memoryviews work on top of it without ...@@ -544,20 +544,24 @@ the buffer interface natively, so memoryviews work on top of it without
additional setup. additional setup.
Starting with Cython 0.17, however, it is possible to use these arrays Starting with Cython 0.17, however, it is possible to use these arrays
as buffer providers also in Python 2. This is done through explicit as buffer providers also in Python 2. This is done through explicitly
typing (e.g. a cast or assignment) as follows:: cimporting the ``cpython.array`` module as follows::
from cpython cimport array cimport cpython.array
def sum_array(array.array arr): # explicit typing required in Python 2 def sum_array(int[:] view):
cdef int[:] view = arr """
>>> from array import array
>>> sum_array( array('i', [1,2,3]) )
6
"""
cdef int total cdef int total
for i in range(view.shape[0]): for i in range(view.shape[0]):
total += view[i] total += view[i]
return total return total
Note that the explicit typing also enables support for the old buffer Note that the cimport also enables the old buffer syntax for the array
syntax for the array type. Therefore, the following also works:: type. Therefore, the following also works::
from cpython cimport array from cpython cimport array
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment