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


Groups > comp.lang.python > #68285

Re: Deep vs. shallow copy?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <zachary.ware@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'operator': 0.03; 'assignment': 0.07; 'context': 0.07; 'skip:` 10': 0.07; 'append': 0.09; 'option,': 0.09; 'rows': 0.09; "'rb')": 0.16; 'copied.': 0.16; 'imported).': 0.16; 'readable': 0.16; 'shallow': 0.16; 'shorthand': 0.16; 'typo': 0.16; 'all.': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'wed,': 0.18; 'alex': 0.19; 'basically': 0.19; 'options.': 0.19; 'example': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; "shouldn't": 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'this:': 0.26; 'second': 0.26; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; '(which': 0.31; 'lines': 0.31; '(maybe': 0.31; 'this.': 0.32; 'reader': 0.33; 'copying': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'complete.': 0.36; 'done,': 0.36; 'done': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'clear': 0.37; 'expected': 0.38; 'skip:o 20': 0.38; 'saves': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'explain': 0.39; 'does': 0.39; '12,': 0.39; "couldn't": 0.39; 'to:addr:python.org': 0.39; 'most': 0.60; 'hope': 0.61; 'new': 0.61; 'show': 0.63; 'subject:. ': 0.67; 'between': 0.67; 'mar': 0.68; 'results': 0.69; 'bitten': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=f8Eo0pGqaAWRNdp4a99sgnd2Ezb4gxveXedAw0eHv2Q=; b=0jdA9iSxXkOnCv4u3MdOwGFLuh3KHdccxgoWhk7KVQ54O0/QpybFhXAOt4rLL8lhxg 9S+7yDfK4/f5FedlZJdTwthHpMSEIE479RS0+EJuuaQFh1zjs0WMO6BzQGygPBRAUZO4 KP2nIg8XEgGQZOurrA0JefYr822JXBOru4KfT56KFaeeDrAyzi4I53nCeJIXt0QBW7X4 gBk88pi0/5nbY9XIEV7kp4ajeGb8LuPm5O/DrvPTKc21Ly/KN9EfNYPiQXu4hOXlQo6E CjCEJgQXknf2vJTYBm0oSeoX44X9O+tTtNay0+W3+v13YKS/nw2IZz7meJ30ahn+Rytw 4YUw==
X-Received by 10.50.43.194 with SMTP id y2mr29593685igl.33.1394636430958; Wed, 12 Mar 2014 08:00:30 -0700 (PDT)
MIME-Version 1.0
Sender zachary.ware@gmail.com
In-Reply-To <53206e6a$0$2886$e4fe514c@news2.news.xs4all.nl>
References <53206e6a$0$2886$e4fe514c@news2.news.xs4all.nl>
From Zachary Ware <zachary.ware+pylist@gmail.com>
Date Wed, 12 Mar 2014 10:00:09 -0500
X-Google-Sender-Auth uWLHCdz3lpbpBunvC4-Pgnukri8
Subject Re: Deep vs. shallow copy?
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Cc Alex van der Spek <zdoor@xs4all.nl>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8093.1394636439.18130.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394636440 news.xs4all.nl 2895 [2001:888:2000:d::a6]:45347
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68285

Show key headers only | View raw


On Wed, Mar 12, 2014 at 9:25 AM, Alex van der Spek <zdoor@xs4all.nl> wrote:
> I think I understand the difference between deep vs. shallow copies but
> I was bitten by this:
>
> with open(os.path.join('path', 'foo.txt', 'rb') as txt:
>      reader = csv.reader(txt)
>      data = [row.append(year) for row in reader]
>
> This does not work although the append does complete. The below works:
>
> with open(os.path.join('path', 'foo.txt', 'rb') as txt:
>      reader = csv.reader(txt)
>      data = [row + [year] for row in reader]
>
> However in this context I am baffled. If someone can explain what is
> going on here, I would be most grateful.

Deep/shallow copying doesn't really come into this.  row.append()
mutates the list (row), it doesn't return a new list.  Like most
in-place/mutating methods in Python, it returns None instead of self
to show that mutation was done, so your listcomp fills `data` with
Nones; there is no copying done at all.  The second example works as
you expected because `row + [year]` results in a new list, which the
listcomp is happy to append to `data`--which does mean that `row` is
copied.

To avoid the copy that the second listcomp is doing (which really
shouldn't be necessary anyway, unless your rows are astronomically
huge), you have a couple of options.  First, you can expand your
listcomp and use append:

   with open(os.path.join('path', 'foo.txt'), 'rb') as txt: # with
your typo fixed ;)
       reader = csv.reader(txt)
       data = []
       for row in reader:
           row.append(year)
           data.append(row)

To me, that's pretty readable and pretty clear about what it's doing.
Then there's this option, which I don't recommend:

   import operator
   with open(os.path.join('path', 'foo.txt'), 'rb') as txt:
       reader = csv.reader(txt)
       data = [operator.iadd(row, [year]) for row in reader]

This works because operator.iadd is basically shorthand for
row.__iadd__([year]), which does return self (otherwise, the
assignment part of `row += [year]` couldn't work).  But, it's not as
clear about what's happening, and only saves a whole two lines (maybe
3 if you already have operator imported).

Hope this helps,
-- 
Zach

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


Thread

Deep vs. shallow copy? Alex van der Spek <zdoor@xs4all.nl> - 2014-03-12 14:25 +0000
  Re: Deep vs. shallow copy? Skip Montanaro <skip@pobox.com> - 2014-03-12 09:48 -0500
  Re: Deep vs. shallow copy? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-03-12 10:00 -0500
    Re: Deep vs. shallow copy? Alex van der Spek <zdoor@xs4all.nl> - 2014-03-12 15:29 +0000
      Re: Deep vs. shallow copy? Wayne Brehaut <wbrehaut@mcsnet.ca> - 2014-03-12 13:06 -0600
      Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-12 23:07 +0000
        Re: Deep vs. shallow copy? Rustom Mody <rustompmody@gmail.com> - 2014-03-12 20:09 -0700
          Re: Deep vs. shallow copy? Ian <hobson42@gmail.com> - 2014-03-13 11:38 +0000
            Re: Deep vs. shallow copy? Rustom Mody <rustompmody@gmail.com> - 2014-03-13 08:28 -0700
              Re: Deep vs. shallow copy? random832@fastmail.us - 2014-03-13 13:25 -0400
        Re: Deep vs. shallow copy? Roy Smith <roy@panix.com> - 2014-03-13 07:44 -0400
          Re: Deep vs. shallow copy? Marko Rauhamaa <marko@pacujo.net> - 2014-03-13 14:27 +0200
            Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-13 23:41 +0000
              Re: Deep vs. shallow copy? Chris Angelico <rosuav@gmail.com> - 2014-03-14 10:55 +1100
                Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-14 01:41 +0000
              Re: Deep vs. shallow copy? Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-13 18:08 -0600
              Re: Deep vs. shallow copy? Chris Angelico <rosuav@gmail.com> - 2014-03-14 11:22 +1100
                Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-14 01:59 +0000
              Re: Deep vs. shallow copy? Rustom Mody <rustompmody@gmail.com> - 2014-03-13 19:57 -0700
                Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-14 04:43 +0000
              Re: Deep vs. shallow copy? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-14 06:17 +0000
          Re: Deep vs. shallow copy? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-13 23:31 +0000

csiph-web