[Numpy-discussion] Issue w/ ndarray.view

Travis E. Oliphant oliphant@enthought....
Wed Sep 10 12:09:24 CDT 2008


Pierre GM wrote:
> All,
> I was fixing MaskedArray.view for masked arrays with flexible type when I ran 
> into a small pb with view.
> The new definition accepts 2 keywords dtype and type. I thought I could easily 
> redefined MaskedArray.view as
>
> def view(self, dtype=None, type=None):
>    output = ndarray.view(self, dtype=dtype, type=type)
>    [some extra lines to view the mask if needed]
>    return output
>
> Unfortunately, that doesn't work: if type is None, I get a ValueError that 
> type must be a subtype of ndarray. Using
> output = ndarray.view(self, dtype=dtype, type=type or ndarray)
> doesn't work either if dtype was already a type, as it complains that type is 
> given twice.
>
> I end up having to test whether dtype is None, type is None, build a tuple of 
> arguments (args in one of (dtype,) or (type,) or (dtype,type) or ()) and 
> parse that to ndarray.view(self, *args).
> That's a tad ugly and irritating. Shouldn't the checks on the arguments dealt 
> with on the C side, instead of having to redo them in Python all the time ?
>   
That would be nicer.   It's just a bit harder to do on the C-side where 
PyArg_ParseTuple is the heavy lifter and creates habits of argument 
acceptance that are different than on the Python side.
> As a side-note, recarray.view still accepts only one argument. Looks like 
> there's a pb of consistency.
>   
Yes, it should be changed also. 

-Travis



More information about the Numpy-discussion mailing list