Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'languages,': 0.04; 'item.': 0.07; 'nested': 0.07; 'happens.': 0.09; 'pointers': 0.09; 'semantics': 0.09; 'sep': 0.09; 'unexpectedly': 0.09; '(the': 0.15; 'essential': 0.15; '24,': 0.16; '3],': 0.16; 'alist': 0.16; 'arrays,': 0.16; 'crop': 0.16; 'first:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'sorts': 0.16; 'subject:Problem': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'received:209.85.214.174': 0.21; 'assignment': 0.22; 'explicit': 0.22; 'references': 0.23; 'seems': 0.23; 'external': 0.24; 'header :In-Reply-To:1': 0.25; 'am,': 0.27; 'language.': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'run': 0.28; 'arrays': 0.29; 'array': 0.29; 'objects': 0.29; 'code': 0.31; 'gets': 0.32; 'cases,': 0.33; 'subject:List': 0.33; 'handle': 0.33; 'to:addr :python-list': 0.33; 'operations': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'clear': 0.35; 'faster': 0.35; 'so,': 0.35; 'similar': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'really': 0.36; 'but': 0.36; 'expensive': 0.36; 'should': 0.36; 'level': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'copying': 0.38; 'fact': 0.38; 'mean': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'high': 0.61; 'real': 0.61; 'first': 0.61; 'truly': 0.62; 'different': 0.63; 'ever': 0.63; 'around,': 0.84; 'concept.': 0.84; 'understand,': 0.84; 'angel': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=krVhA4mX76Q38gg0fMjnvTU93LKsG+4/udPUfHOIUnY=; b=ZkoBw5BofKfHn5NNAhCl58TeXYh0Ca4FN0a+V8YUJYYJoU0b+bFnOBi3jWfjg/izgp wnYXhTR+E9h0nxFfhyhT2LxReWEw1SocEpGJ+5TNCCYenTElhl9iOT+eCnkjARGKIzVA d/52uMOQTg8QkL6+77FzJk85LqUjO3n5MMPjhblO/uaV2mfGlCFUNOMJPPcSU1RIfRe1 xU1HuMTFpkLXa7oydtwWvbJpWyX4s4Pl9lLddeKxPmTUQF+y1OD41WiRUbxTtawKHoRH 1qEezMA/pIz7MGkHSJZDzVHkMZAntDJFhMUQYQwI0EQ+f27wu4Crx/c7ZIquGREbxe4J UplA== MIME-Version: 1.0 In-Reply-To: <505F8734.2020908@davea.name> References: <5126348a-8e87-493d-975c-d6273e59784c@googlegroups.com> <505F8734.2020908@davea.name> Date: Mon, 24 Sep 2012 08:27:28 +1000 Subject: Re: List Problem From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348439251 news.xs4all.nl 6975 [2001:888:2000:d::a6]:41167 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29865 On Mon, Sep 24, 2012 at 8:03 AM, Dave Angel wrote: > blist = [alist, alist, alist] # or blist = alist * 3 (Minor point: I think you mean this.) # or blist = [alist] * 3 > Understand, this is NOT a flaw in the language. It's perfectly > reasonable to be able to do so, in fact essential in many cases, when > you want it to be the SAME item. And this is the real part. There's no other way to handle complex objects that makes as much sense. PHP's system of references and copy-on-write assignment doesn't truly cover all cases, and it can make operations unexpectedly run vastly faster or slower depending on external circumstances, which gets annoying (the first write to an assigned array has to copy the array). C simply doesn't let you move arrays around, only pointers to them, so semantics are actually pretty similar to high level languages, only in a completely different way. These sorts of issues only ever seem to crop up with nested arrays, which strengthens this next point: Deep copying is a really REALLY hairy concept. It seems so simple at first: a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] b = deepcopy(a) # b is a new list with three new sublists But then it gets messy. a = [[1, 2, 3]]*2 + [[7, 8, 9]] It's much better to make copying a very explicit thing; it's so expensive that you really should make it very clear in your code when this happens. ChrisA