Subclassing numarray's arrays
Mizrandir
mizrandir at hotmail.com
Sun Aug 15 05:11:57 EDT 2004
First of all, thanks for helping.
> Numarray wasn't designed with subclassing in mind, but there are
> workarounds for most problems.
On numarray's website there is a paper by the Numarray authors saying:
"... The new approach allows arrays to be subclassed as well as ...",
so I thought it should be something easy to do.
Anyway, I hadn't read anything about numarraycore, is it explained
anywhere. For example, I don't understand what the statement
"self.__setstate__(arr.__getstate__())" does.
> You might try something like:
> import numarray.numarraycore as _num
> class NewClass(_num.NumArray):
> def __init__(self, n, a):
> ''' n provides the length of each dimension,
> a is the constant value to be plugged.
> '''
> arr= _num.array(sequence= n * n * [a], shape= (n, n))
> self.__setstate__(arr.__getstate__())
>
> def __repr__(self):
> " Return printable representation of instance."
> className= self.__class__.__name__
> className= className.zfill(5).replace('0', ' ')
> arr= self.copy()
> arr.__class__= _num.NumArray
> rep= className + _num.NumArray.__repr__(arr)[5:]
> return rep
>
> def __str__(self):
> " Return a pretty printed string of the instance."
> stri= self.copy()
> stri.__class__= _num.NumArray
> return _num.NumArray.__str__(stri)
>
>
> if __name__ == '__main__':
> a= NewClass(2, 2)
> print a
> b= NewClass(3, 4)
> print `b`
miz.
More information about the Python-list
mailing list