numpy继承ndarray对象变异

时间:2016-07-26 09:21:07

标签: python numpy inheritance mutation

我从numpy ndarray继承并尝试添加追加删除方法。 不知何故,这些方法不会改变对象。欢迎任何提示或解决方案。我班级的重要部分看起来像

import numpy as np

class Points3D(np.ndarray):
"""
Points3D is a general class for 3D Point operations and storage.
"""
CARTESIAN = "cartesian"
CYLINDER = "cylinder"
__slots__ = ['coordSys']

def __new__(cls, inputArray):
    if inputArray == []:
        inputArray = np.empty((0, 3))
    array = np.asarray(inputArray, dtype=np.float64)
    if 3 not in array.shape and inputArray:
        raise AttributeError("Input array must have one dimension equals 3. Its shape is %s though" % array.shape)
    if array.shape[0] == 3 and not array.shape[1] == 3:
        array = array.T
    obj = array.view(cls)
    return obj

def __init__(self, *args, **kwargs):
    self._setCoordSys(kwargs.pop("coordSys", self.CARTESIAN))

def remove(self, point):
    """
    remove first occurrence of point.
    """
    # self = np.delete(self, self.indices(point)[0]) #will not work of course
    #but something like that

def indices(self, point):
    indices = []
    for i, p in enumerate(self):
        if all(p == point):
            indices.append(i)
    return indices

def _setCoordSys(self, coordSys):
    """
    Args:
        coordSys (str): see above __init__ for the constants you can set.
    """
    self.coordSys = coordSys

我想知道是否必须实现 array_wrap 等方法。 如果有一个例子,那将会有很大帮助。 干杯, 丹尼尔

0 个答案:

没有答案