Robert Bradshaw wrote: > On Oct 8, 2009, at 6:04 AM, Dag Sverre Seljebotn wrote: > >> I experience this: >> >> <Py_ssize_t>np.sqrt(some_float64) >> => TypeError: 'numpy.float64' object cannot be interpreted as an >> index >> >> However >> a) <int>np.sqrt(...) works fine, as does <long> and <long long>. >> b) In Python, int(np.sqrt(...)) and long(np.sqrt(...)) both works >> fine >> >> Is Py_ssize_t "special" when it comes to converting from Python >> objects? >> If so I find it mildly confusing and something which only increases >> learning curve... > > Py_ssize_t is meant for indexing, and fails for the same reason that > > >>> L = range(10) > >>> L[2] > 2 > >>> L[2.5] > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: list indices must be integers, not float > > fails. This is a feature, not a bug.
OK, if this is a feature, help me out with this then: I need to iterate from 0 to "lmax". When I allocate an array, it is always allocated of size (lmax + 1)**2, and it is convenient to be able to deduce lmax from the array shape. What type should I use for lmax to conveniently convert from a float (because sqrt is float, but I know I can safely truncate) and guarantee that I support the range of NumPy arrays (which essentially use Py_ssize_t to store shape info)? The type must be signed, otherwise I fall into the range(-lmax, lmax) trap all the time. (For the record, I think the type system is complicated enough without this feature -- does it have usecases where an explicit call to e.g. a cython.asindex() function to invoke __index__ isn't clearer?) -- Dag Sverre _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
