[Cython] Cython array type: Summary, introducing CEP 518
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Thu Jun 18 21:11:10 CEST 2009
Dag Sverre Seljebotn wrote:
> Stefan Behnel wrote:
>> Dag Sverre Seljebotn wrote:
>>>> # Some ways of multiplying all elements with 2
>>>> x *= 2
>>>> x[...] *= 2
>>>> x[:,:] *= 2
>>>> x += x
>>>> x[...] += x
>>> OK I'll make an exception here -- I'm willing to discuss whether we
>>> should depart from NumPy semantics here and let
>>>
>>> x2 = x
>>> x *= 2
>>>
>>> allocate new memory, so that x2 is not modified, being consistent with a
>>> direct transformation to "x = x * 2". One can always write
>>>
>>> x[...] *= 2
>>>
>>> if one wishes to modify original memory.
>> What's wrong with
>>
>> x = 2 * x2
>>
>> for doing a copy ?
>>
>> x *= 2
>>
>> pretty clearly states that I want to modify x in place.
>
> The problem is that if you do
>
> y = x
> x *= 2
>
> then in current NumPy, y and x will still point to the same memory and
> reference the same values (in fact, be the exact same view object).
>
> Usual Python semantics seems to imply that what NumPy *should* have done
> is let the latter line mean "x = x * 2", where a new array is allocated
> and the value of x*2 copied into the new array, so that x and y points
> to different memory after the operation.
I stand corrected:
In [13]: a = [1,2,3]
In [14]: b = a
In [15]: a += [1,2,3]
In [16]: b
Out[16]: [1, 2, 3, 1, 2, 3]
OK, so this seems like a non-issue.
--
Dag Sverre
More information about the Cython-dev
mailing list