I'm trying to create a numpy scalar of a specified dtype. I know I could do, say, x = numpy.int16(3), but I don't know the dtype in advance.
If I were to want an array then
dtype = int
x = numpy.array(3, dtype=dtype)
would do it, so I had high hopes for
x = numpy.generic(3, dtype=dtype)
but one cannot create an instance of numpy.generic.
Any ideas?
x = dtype(3)do what you want?int(3)instead ofnumpy.int32(3). Whether that's an issue or not, I don't know.dtype;intwill giveint,np.int16will givenp.int16. I must be missing something, though, because the OP knows that you can call the type object (in the first line) and also that you can bind the type objects todtype.a = np.eye(2, dtype=int),b = np.zeros(2, dtype=a.dtype), soa.dtypecan be passed as a dtype argument, buta.dtype(3)gives a not-callable error. The.typemethod does what I need ("make dtypes callable").