[Numpy-discussion] Re: array constructor from generators?

Robert Kern robert.kern at gmail.com
Tue Apr 4 17:50:05 CDT 2006


Zachary Pincus wrote:

> The downside is that currently, passing array() an object makes a 0-d 
> object array with that object. If this were changed, then passing 
> array() an iterator object would be handled differently than passing 
> array any other object. This might possibly be a fatal flaw in this  idea.

I don't think so. We can pass appropriate lists to array(), and it handles them
fine. Iterator objects are just another kind of object that gets special
treatment. The tricky bit is recognizing them.

> I'd be happy to look in to implementing this functionality if people 
> think it is a good idea, and could give me some tips as to the best  way
> to implement it.

I think a prerequisite for turning an arbitrary iterable into a numpy array is
to iterate over it and store all of the objects in a temporary buffer that
expands with a sensible strategy. I can't think of a better buffer object than
regular Python lists.

I think you can recognize when you have to use the temporary list strategy by
seeing if the input has .__iter__() but not .__len__(). I'd have to refresh
myself on the details of PyArray_New to be more sure, though.

As Tim suggests, 2.5's __length_hint__ will also help.

Another note of caution: You are going to have to deal with iterators of
iterators of iterators of.... I'm not sure if that actually overly complicates
matters; I haven't looked at PyArray_New for some time. Enjoy!

-- 
Robert Kern
robert.kern at gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco





More information about the Numpy-discussion mailing list