[SciPy-user] NumPy arrays of Python objects (it was Re: How to start with SciPy and NumPy)

Vicent vginer at gmail.com
Mon Jan 26 03:45:50 EST 2009


On Mon, Jan 26, 2009 at 09:02, Pierre GM <pgmdevlist at gmail.com> wrote:

> Vicent,
> 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.
> P.
>



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)
>>> type(a.property1)
<type 'float'>
>>> type(b.property1)
<type 'int'>
>>> type(c.property1)
<type 'bool'>
>>> 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
see.

I hope now it's clearer...

--
Vicent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090126/20b2df5d/attachment.html>


More information about the SciPy-User mailing list