Assigning generator expressions to ctype arrays

Patrick Maupin pmaupin at gmail.com
Thu Oct 27 20:09:34 EDT 2011


On Oct 27, 5:31 pm, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> From the outside, you can't tell how big a generator expression is. It has no length:

I understand that.

> Since the array object has no way of telling whether the generator will have the correct size, it refuses to guess.

It doesn't have to guess.  It can assume that I, the programmer, know
what the heck I am doing, and then validate that assumption -- trust,
but verify.  It merely needs to fill the slice and then ask for one
more and check that StopIteration is raised.

> I would argue that it should raise a TypeError
> with a less misleading error message, rather
> than a ValueError, so "bug".

And I would argue that it should simply work, unless someone can
present a more compelling reason why not.

> The simple solution is to use a list comp
> instead of a generator expression.

I know how to work around the issue.  I'm not sure I should have to.
It violates the principle of least surprise for the ctypes array to
not be able to interoperate with the iterator protocol in this
fashion.

Regards,
Pat



More information about the Python-list mailing list