Two-dimensional arrays

Carl Banks imbosol at aerojockey.com
Tue May 27 14:27:38 EDT 2003


Terry Reedy wrote:
>> 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.

If you think Purity beats Practicality, then there certainly nothing
to be embarrassed about.

But the Python Zen is "Practicality beats Purity," and it is currently
impractical to create a multidimensional array.  I wouldn't go so far
as to call it embarrassing, but it certainly is inconvenient.  Python
makes it easy to do something that isn't useful (create a list of
references to the same sequence), and hard to do something that is.
That's a problem, and that it "acts entirely consistently with its
object model and its advertised syntax" doens't make it any less of
one.


>> Should this issue be worth another PEP?
> 
> What is the 'improvement' proposal?

I think a builtin multidimensional array factory would be nice.  I
suggest this:

    multiarray(dims,initializer=None,func=None)

dims is a sequence of array dimensions.

initializer is an initializer value.

func is a function which sets the values in the array.  It takes
len(dims)+1 arguments: the first n arguments being the array indices,
and the last argument being the initializer.

If func is None, then initialize all values in the array to
initializer.


>> 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.
> 
> 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.

Yes, but despite this, most of those languages have convenient ways to
create multidimensional arrays without 'aliasing'.  Python doesn't,
and that's the real problem here.


-- 
CARL BANKS




More information about the Python-list mailing list