[SciPy-user] NumPy arrays of Python objects (it was Re: How to start with SciPy and NumPy)
Mon Jan 26 02:45:50 CST 2009
On Mon, Jan 26, 2009 at 09:02, Pierre GM <firstname.lastname@example.org> wrote:
> Without a more specific example, it might be quite difficult for us to
> help you.
> Would your 'value' property be of the same type for all the objects of
> your sequence ?
No, that's what I meant.
> If yes, then you could define a class where 'value'
> would be a ndarray. Other properties would then be other arrays, and
> so forth.
> But I probably speak out of place.
Ok, this is an example of what I am referring to. The class is called
"Element", and the property is called "property1" (and not "value", which
can be confusing):
>>> import numpy as N
>>> class Element :
... def __init__(self, value) :
... self.property1 = value
>>> a = Element(1.)
>>> b = Element(1)
>>> c = Element(True)
>>> alltog = N.array([a,b,c])
The "alltog" array has 3 members, or elements, or scalars... each of them
being objects from the "Element" class, although each of them "contains" a
different type of value in its "property1".
[ I know that "property1" is just like a "pointer" (more or less), so I
understand that the objects named by "a", "b" and "c" don't "contain" any
number,actually. Is like that, isn't it? ]
My (multiple) question is:
Is that a "bad" (not optimal) implementation, because I am mixing NumPy
"optimized" arrays with "simple" objects? Would it be better if each element
in the array was a "record" built by using NumPy "dtype" feature? I think I
can't, because each value in "property1" can have a different type, as you
I hope now it's clearer...
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the SciPy-user