I'm creating a class that inherits from a NumPy ndarray. I'm having a bit of trouble giving it methods. Specifically, when I add a simple method printout to the class, I get the following error:
AttributeError: 'NoneType' object has no attribute 'printout'
How should I be adding methods to this class? Also, preemptively, is there a recommended way to change the value of self internally in this class?
import numpy
class Variable(numpy.ndarray):
def __new__(
cls,
name = "trk_pt",
tree = None, # tree object
eventNumber = None,
eventWeight = None,
numberOfBins = None, # binning
binningLogicSystem = None, # binning
):
self = numpy.asarray([]).view(cls)
# arguments
self._name = name
self.tree = tree
self.eventNumber = eventNumber
self.eventWeight = eventWeight
self.numberOfBins = numberOfBins
self.binningLogicSystem = binningLogicSystem
# internal
self.variableObject = None
self.variableType = None
self.dataType = None
self.variableDataTypes = None
self.canvas = None
self.histogram = None
self._values = [] # list of values
self._valuesRaw = [] # list of unmodified, raw values
def printout(
self
):
print("hello world")
a = Variable()
a.printout()
.viewdoes not return anything__new__, you have to return the newly created instance of your class.