[Python-Dev] Error in PEP3118?
Travis Oliphant
oliphant.travis at ieee.org
Wed Jan 23 17:17:01 CET 2008
Thomas Heller wrote:
> Hi Travis,
>
> The pep contains this sample:
>
> """
> Nested array
> ::
>
> struct {
> int ival;
> double data[16*4];
> }
> """i:ival:
> (16,4)d:data:
> """
> """
>
> I think it is wrong and must be changed to the following; is this correct?
>
> """
> Nested array
> ::
>
> struct {
> int ival;
> double data[16][4];
> }
> """i:ival:
> (16,4)d:data:
> """
> """
I responded off list to this email and wanted to summarize my response
for others to peruse.
Basically, the answer is that the struct syntax proposed for
multi-dimensional arrays is not intended to mimic how the C-compiler
handles statically defined C-arrays (i.e. the pointer-to-pointers style
of multi-dimensional arrays). It is intended to handle the
contiguous-block-of-data style of multi-dimensional arrays that NumPy uses.
I wanted to avoid 2-d static arrays in the examples because it gets
confusing and AFAIK the layout of the memory for a double data[16][4] is
the same as data[16*4]. The only difference is how the C-compiler
translates data[4][3] and data[4].
The intent of the struct syntax is to handle describing memory. The
point is not to replicate how the C-compiler deals with statically
defined N-D arrays. Thus, even though the struct syntax allows
*communicating* the intent of a contiguous block of memory inside a
structure as an N-d array, the fundamental memory block is the
equivalent of a 1-d array in C.
So, I think the example is correct (and intentional).
-Travis O.
More information about the Python-Dev
mailing list