[Numpy-discussion] python user defined type
Nicolas Rougier
Nicolas.Rougier@loria...
Thu Jul 10 13:48:18 CDT 2008
Hi all,
I'm rather new to the list so maybe the question is well known but from
my researches on the web and list archive, I did not find a clear
explanation.
I would like to create numpy array with my own (python) datatype, so I
tried the obvious solution:
from numpy import *
class Unit(object):
def __init__(self,value=0.0):
self.value = value
def __float__(self):
return self.value
def __repr__(self):
return str(self.value)
a = array([[Unit(1), Unit(2)], [Unit(3), Unit(4)]])
print a
print a.dtype.name
print a[0,0].value
#a[0,0] = 0
#print a[0,0].value
a = array (array([[1,2],[3,4]]), dtype=Unit)
print a
print a.dtype
print a[0,0].value
but the last print make python to raise an AttributeError stating that
int object do not have a 'value' attribute. I guess I missed something
concerning numpy object data type. Do I need to implement some special
methods in Unit to make numpy happy ?
Also, the commented line (a[0,0] = 0) makes the item to become an int
while I need to set the value of the item instead, is that possible ?
Nicolas
More information about the Numpy-discussion
mailing list