Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'none,': 0.05; 'result,': 0.05; 'semantic': 0.07; 'suppose': 0.07; 'python': 0.09; 'argument,': 0.09; 'pointers': 0.09; 'sake': 0.09; 'sub': 0.09; 'do,': 0.15; 'weird': 0.15; '*any*': 0.16; '*only*': 0.16; '[none]': 0.16; 'behaviour.': 0.16; 'benefit.': 0.16; 'confusion': 0.16; 'copied.': 0.16; 'nest': 0.16; 'objects;': 0.16; 'shallow': 0.16; 'string': 0.17; 'wrote:': 0.17; 'certainly': 0.17; 'comparing': 0.17; 'copied': 0.17; 'replacing': 0.17; 'changes': 0.20; 'meant': 0.21; 'assignment': 0.22; 'made.': 0.22; 'subject:skip:i 10': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'andrew': 0.27; 'change,': 0.27; 'object,': 0.27; 'subject:list': 0.28; 'all.': 0.28; 'chris': 0.28; 'initial': 0.28; 'noticed': 0.28; 'strings,': 0.29; 'time;': 0.29; 'objects': 0.29; "i'm": 0.29; 'lists': 0.31; 'point': 0.31; 'could': 0.32; "aren't": 0.33; 'to:addr:python- list': 0.33; 'themselves': 0.33; 'another': 0.33; 'list': 0.35; 'lists.': 0.35; 'locations': 0.35; 'nov': 0.35; 'doing': 0.35; 'pm,': 0.35; 'there': 0.35; 'really': 0.36; 'created': 0.36; 'but': 0.36; 'one,': 0.37; 'level': 0.37; 'item': 0.37; 'usual': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'copying': 0.38; 'object': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'subject:-': 0.40; 'help': 0.40; 'think': 0.40; 'received:network': 0.61; 'received:phx3.secureserver.net': 0.62; 'received:prod.phx3.secureserver.net': 0.62; 'between': 0.63; 'different': 0.63; 'received:unknown': 0.63; 'times': 0.63; 'more': 0.63; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'different.': 0.84; 'down:': 0.84; "it'd": 0.84; 'affected.': 0.91; 'received:173.201': 0.91 Date: Tue, 06 Nov 2012 00:21:07 -0800 From: Andrew Robinson User-Agent: Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111126 Thunderbird/8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Multi-dimensional list initialization References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: andrew3@r3dsolutions.com 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352190429 news.xs4all.nl 6882 [2001:888:2000:d::a6]:40810 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32808 On 11/05/2012 10:07 PM, Chris Angelico wrote: > On Tue, Nov 6, 2012 at 4:51 PM, Andrew Robinson > wrote: >> I really don't think doing a shallow copy of lists would break anyone's >> program. > Well, it's a change, a semantic change. It's almost certainly going to > break _something_. But for the sake of argument, we can suppose that > the change could be made. Would it be the right thing to do? > > Shallow copying by default would result in extremely weird behaviour. > All the same confusion would result, only instead of comparing > [None]*4 with [[None]]*4, there'd be confusion over the difference > between [[None]]*4 and [[[None]]]*4. > > I don't think it would help anything, and it'd result in a lot more > work for no benefit. > > ChrisA I don't follow. a=[ None ]*4 would give a=[ None, None, None, None ] as usual. All four None's would be the same object, but there are automatically 4 different pointers to it. Hence, a[0]=1 would give a=[ 1, None, None, None ] as usual. a=[ [None] ]*4 would give a=[ [None], [None], [None], [None] ] as usual BUT: a[0][0] = 1 would no longer give a=[ [1],[1],[1],[1] ] *Rather* it would give a=[ [1].[None].[None],[None] ] The None objects are all still the same one, BUT the lists themselves are different. Again, a=[ ["alpha","beta"] * 4 ] would give: a=[ ["alpha","beta"], ["alpha","beta"], ["alpha","beta"], ["alpha","beta"] ] All four strings, "alpha", are the same object -- but there are 5 different lists; The pointers inside the initial list are copied four times -- not the string objects; But the *lists* themselves are created new for each replication. If you nest it another time; [[[None]]]*4, the same would happen; all lists would be independent -- but the objects which aren't lists would be refrenced-- not copied. a=[[["alpha","beta"]]]*4 would yield: a=[[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']]] and a[0][0]=1 would give [[1],[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']]]] rather than a=[[1], [1], [1], [1]] Or at another level down: a[0][0][0]=1 would give: a=[[[1, 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']] ] rather than a=[[[1, 'beta']], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] The point is, there would be no difference at all noticed in what data is found where in the array; the *only* thing that would change is that replacing an item by assignment would only affect the *location* assigned to -- all other locations would not be affected. That really is what people *generally* want. If the entire list is meant to be read only -- the change would affect *nothing* at all. See if you can find *any* python program where people desired the multiplication to have the die effect that changing an object in one of the sub lists -- changes all the objects in the other sub lists. I'm sure you're not going to find it -- and even if you do, it's going to be 1 program in 1000's.