[Numpy-discussion] array, asarray as contiguous and friends

Tim Hochberg tim.hochberg at cox.net
Thu Mar 23 15:07:04 EST 2006


Sasha wrote:

>On 3/23/06, Travis Oliphant <oliphant at ee.byu.edu> wrote:
>  
>
>>Thus, in my mind, the array call should get another flag keyword that
>>forces a contiguous result.
>>    
>>
>
>Please don't! The fortran flag is bad enough, but has too much history
>behind it.  Let's not breed boolean parameters. Sooner or later
>someone will use keword arguments positionally and you will end up
>guessing what
>
>array([1,2], int8_, 1, 1, 0, 0)
>
>means.
>  
>

We could always force keyword parameter by using **kwd. That would mess 
mess up help() and other introspection tools. Then again, if we want to 
discourage overuse of this sort of stuff, perhaps that's not a bad thing ;)

But if we really need access to the C guts of an array, just allow a 
dictionary of flags to get plugged in. These would be the same as what 
somearray.flags puts out. The new interface for array would be:

    array(object, **flags)

Where flags could contain:

      CONTIGUOUS : force a copy if object isn't contiguous if
    True.         [default None]
      FORTRAN : force array to be fortran order if True, C order if
    False [default None]
      OWNDATA : force a copy if
    True                                                      [default True]
      WRITEABLE :  Force a copy if object isn't writeable if Tru 
                [default None]
      ALIGNED : Force a copy if object isn't aligned if True.          
              [default None]
      UPDATEIFCOPY : Set the UPDATEIFCOPY
    flag?                           [default ???]

With the exception of the FORTRAN, and possibly UPDATEIFCOPY, it would 
be an error to set any of these flags to False (Forcing an array to be 
discontiguous for instance, makes no sense).

That's a thin interface and it ties together with the flags parameter 
nicely. On the downside, it's a little weird, particularly using OWNDATA 
for copy, although it is logical once you think about it. It also drops 
'minnd' and 'subok'. I wouldn't miss them, but I expect someone would 
squawk. You could shoehorn them into flags, but then you lose one of the 
chief virtues of this scheme, which is that it makes a strong connection 
between the constructor and the flags parameter. 'subok' should be 
pretty well taken care of by 'asanyarray' and it would be easy enough to 
create an auxilliary function to replicate the 'minnd' functionality.

With this kind of narrow interface, it might make sense to allow the 
flags parameter on all of the asX functions. That makes for a nice 
uniform, easy to remember, interface. I'm not positive I like this idea 
yet, but I thought it was interesting enough to throw into the ring anyway.

Tangentially, I noticed that I can set ALIGNED to False. Isn't that 
going to break something somewhere? Should the ALIGNED flag be readonly?

-tim









More information about the NumPy-Discussion mailing list