Two-dimensional arrays

monsterkodi monsterkodi at gmx.net
Tue May 27 13:03:43 EDT 2003


Hello Terry,

> > Anybody else here who thinks that this is one of the very few
>  embarrasing sides of
> > python?
> 
> What do you think anyone should be embarassed about?  In this respect,
> Python acts entirely consistently with its object model and its
> advertised syntax.

I just think that this way of constructing a multidimensional array
isn't exactly what I would call a 'very clear and intuitive syntax'.
Normally I find python very intuitive, but even the shortest of the
proposed solutions doesn't look intuitive to me. Probably
'embarassing' wasn't the right word, let's say I feel a little bit
'unhappy' about this 'problem'.

> > Should this issue be worth another PEP?
> 
> What is the 'improvement' proposal?
> 
> > Am I missing some deeper knowledge which would enable me to see the
>  problem
> > in providing 'low level' 'c-stylish' 'built-in' multidimensional
> list features?
> 
> As I remember, C, unlike Fortran, does not have builtin
> multidimensional arrays -- just arrays of arrays, much like Python.
> So it is hard to know from the above what you think you are
> suggesting.

I am not sure myself what would be a good solution so there isn't much
I really can suggest.
Maybe a built in function 
array(width,height,depth=0,value=None)
which returns a simple 2 or 3-dimensional array, internally
represented
as lists of lists (of lists)

Of course I can write it myself quite easily, 

> If, in C, you do the equivalent to what you did and initialize an
> array of pointer to array with the same pointer to the same array, so
> that the inner array is 'aliased' multiple times, you will get the
> same 'disconcerting' behaviour that changing the structure changes it,
> regarless of how you subsequently look at it.  Any language that
> allows aliasing -- multiple access paths to one mutable data
> structure -- can have the same newcomer surprise.

You are right, self referencing datastructures like the one you are
describing  can be quite hard to understand and debug, independent of
the language. That's why one will normally avoid creating them 'by
accident'.




More information about the Python-list mailing list