
Jan. 8, 2025
6:21 p.m.
From time to time I find myself overwriting a python buffer with the output of a ufunc, for example like this: import array import numpy as np a = array.array('f', (1,1,1)) np.exp2(a, out=np.asarray(a)) assert a.tolist() == [2, 2, 2] Here I have to wrap `out=np.asarray(a)` because the more natural `np.exp2(a, out=a)` raises "TypeError: return arrays must be of ArrayType” In general, ufuncs are quite aggressive in utilizing the buffer protocol for input arguments. I was wondering, why are they so reluctant to do the same for the output argument? Is this a design choice? Efficiency? Legacy? Would be implementing `np.ufunc(a, out=a)` dangerous or cumbersome? Stefano