Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92141

Re: So what's happening here?

From Peter Otten <__peter__@web.de>
Subject Re: So what's happening here?
Date 2015-06-05 15:09 +0200
Organization None
References <pan.2015.06.05.12.46.21@nowhere.invalid>
Newsgroups comp.lang.python
Message-ID <mailman.197.1433509766.13271.python-list@python.org> (permalink)

Show all headers | View raw


Paul Appleby wrote:

> I saw somewhere on the net that you can copy a list with slicing. So
> what's happening when I try it with a numpy array?
> 
>>>> a = numpy.array([1,2,3])
>>>> b = a[:]
>>>> a is b
> False
>>>> b[1] = 9
>>>> a
> array([1, 9, 3])

Copy or view -- have a look under the hood:

>>> a = numpy.array([1,2,3])
>>> v = a.view()
>>> c = a.copy()
>>> s = a[:]
>>> a.flags["OWNDATA"]
True
>>> v.flags["OWNDATA"]
False
>>> c.flags["OWNDATA"]
True
>>> s.flags["OWNDATA"]
False

You only get a copy if you ask for one; slicing produces a view.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

So what's happening here? Paul Appleby <pap@nowhere.invalid> - 2015-06-05 12:46 +0000
  Re: So what's happening here? Fabien <fabien.maussion@gmail.com> - 2015-06-05 14:52 +0200
  Re: So what's happening here? Larry Martell <larry.martell@gmail.com> - 2015-06-05 08:55 -0400
  Re: So what's happening here? Peter Otten <__peter__@web.de> - 2015-06-05 15:09 +0200
  Re: So what's happening here? Paul Appleby <pap@nowhere.invalid> - 2015-06-05 13:11 +0000
    Re: So what's happening here? Gary Herron <gary.herron@islandtraining.com> - 2015-06-05 06:23 -0700
    Re: So what's happening here? Steven D'Aprano <steve@pearwood.info> - 2015-06-05 23:51 +1000
    Re: So what's happening here? Nobody <nobody@nowhere.invalid> - 2015-06-05 15:13 +0100

csiph-web