<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hu Luc,<div><br></div><div><div>On Apr 5, 2013, at 6:46 AM, Luc Bourhis wrote:</div><blockquote type="cite"><div>However, we haven't address the important question: what behaviour do we really want?<br>(i) the numpy array and the flex array must have the same element type, and an exception is thrown if this precondition is violated;<br>(ii) we use a fast copy of the numpy array to the flex array if the element types are the same; otherwise we fall back to a slower conversion.</div></blockquote></div><div><br></div><div>My understanding of the "python way" would be that the desired behavior is (ii). I usually base these types of conclusions on how the python interpreter behaves, and, if that isn't specific enough, I look to the standard library.</div><div><br></div><div>Consider the behavior of python's built-in array, which works like lists:</div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font class="Apple-style-span" face="Monaco">py&gt; import array</font></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; array.array('f', [1, 2, 3])</font></div></div><div><div><font class="Apple-style-span" face="Monaco">array('f', [1.0, 2.0, 3.0])</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; array.array('f', flex.bool([True, False, False]))</font></div></div><div><div><font class="Apple-style-span" face="Monaco">array('f', [1.0, 0.0, 0.0])</font></div></div><div><font class="Apple-style-span" face="Monaco"><div>py&gt; array.array('i', numpy.arange(4))</div><div>array('i', [0, 1, 2, 3])</div></font></div></blockquote><div><br></div><div>Python arrays and lists don't care what type of elements comprise the data structures they are built from. For example, with array, the constructor doesn't care what the elements of the second argument are as long as they behave as specified in the first: integers can be converted to floats, bools to ints, numpy.int64s can be converted to ints, etc. The "python way" is concerned with behavior rather than type (usually at the sacrifice of performance, which is purposefully an afterthought).</div><div><br></div><div>Besides being the "python way", I actually think that such behavior is often easier to implement. Numpy arrays already contain all of the information that describes how they are built. And furthermore, they are able to be converted to any other type without the need to inspect their innards. So the programmer only really needs to know what type of array he wants, and to avoid decisions based on what he has (which would be required in order to throw an error for every "illegal" type).</div><div><br></div><div>Taking the "pythonic" approach only requires</div><div><br></div><div>&nbsp; &nbsp; <a href="http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_CastToType">http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_CastToType</a></div><div><br></div><div>For each of the element types that could comprise flex arrays, one only needs to write one PyArray_Descr</div><div><br></div><div>&nbsp; &nbsp;<a href="http://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html#PyArray_Descr">http://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html#PyArray_Descr</a></div><div><br></div><div>which becomes the second argument for PyArray_CastToType.</div><div><br></div><div>James</div><div><br></div><div><br></div></body></html>