[Numpy-discussion] Use my own data type with NumPy
Christopher Barker
Chris.Barker at noaa.gov
Wed Sep 5 16:28:28 EDT 2007
Günter Dannoritzer wrote:
> The purpose of my (Python) class is to model a fixed point data type. So
> I can specify how many bits are used for integer and how many bits are
> used for fractional representation.
> it would be possible to create a
> list of my FixedPoint instances and then assign that list to a numpy array.
If your class looks like a sequence, it's going to confuse numpy.array()
> I created some minimal code that shows the behavior:
>
> import numpy
>
> class FixPoint(object):
> def __repr__(self):
> return "Hello"
>
> def __len__(self):
> return 3
>
> def __getitem__(self, key):
> return 7
Why would a FixPoint object have to look like a sequence, with a length
and a _getitem_? That's where the confusion is coming from.
If I understand your needs, a FixPoint object is a number -- you'll want
to override __add__ __mult__, and all those sorts of things, but there
should be no need to make it look like a sequence.
What does the "length" of a fixed point number mean? What does it meant
to get the third element of a fixed point object?
I'm guessing that maybe you're using __len__ to mean bit-width, but I'd
just have a property for that, and call it BitWidth or something.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list