[Numpy-discussion] Issue w/ ndarray.view

Pierre GM pgmdevlist@gmail....
Wed Sep 10 10:17:10 CDT 2008

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 ?

As a side-note, recarray.view still accepts only one argument. Looks like 
there's a pb of consistency.

Looking forward to reading your comments

More information about the Numpy-discussion mailing list