[Numpy-discussion] np.array execution path

Sebastian Haase seb.haase at gmail.com
Sat Sep 22 16:00:30 EDT 2012


Oh,
is this actually documented - I knew that np.array would (by default)
only create copies as need ... but I never knew it would - if all fits
- even just return the original Python-object...

Thanks,
Sebastian Haase

On Sat, Sep 22, 2012 at 8:12 PM, Travis Oliphant <travis at continuum.io> wrote:
> Check to see if this expression is true
>
> no is o
>
> In the first case no and o are the same object
>
>
> Travis
>
> --
> Travis Oliphant
> (on a mobile)
> 512-826-7480
>
>
> On Sep 22, 2012, at 1:01 PM, Sebastian Berg <sebastian at sipsolutions.net> wrote:
>
>> Hi,
>>
>> I have a bit of trouble figuring this out. I would have expected
>> np.asarray(array) to go through ctors, PyArray_NewFromArray, but it
>> seems to me it does not, so which execution path is exactly taken here?
>> The reason I am asking is that I want to figure out this behavior/bug,
>> and I really am not sure which function is responsible:
>>
>> In [69]: o = np.ones(3)
>>
>> In [70]: no = np.asarray(o, order='C')
>>
>> In [71]: no[:] = 10
>>
>> In [72]: o # OK, o was changed in place:
>> Out[72]: array([ 10.,  10.,  10.])
>>
>> In [73]: no.flags # But no claims to own its data!
>> Out[73]:
>>  C_CONTIGUOUS : True
>>  F_CONTIGUOUS : True
>>  OWNDATA : True
>>  WRITEABLE : True
>>  ALIGNED : True
>>  UPDATEIFCOPY : False
>>
>> In [74]: no = np.asarray(o, order='F')
>>
>> In [75]: no[:] = 11
>>
>> In [76]: o # Here asarray actually returned a real copy!
>> Out[76]: array([ 10.,  10.,  10.])
>>
>>
>> Thanks,
>>
>> Sebastian
>>
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list