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: 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 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" Content-Type: text/plain; charset=UTF-8 Cc: Alex van der Spek X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Mar 12, 2014 at 9:25 AM, Alex van der Spek 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