Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'operator': 0.03; 'none,': 0.05; 'received:64.202.165': 0.07; 'python': 0.09; 'compact': 0.09; 'logic': 0.09; 'modifies': 0.09; 'notation': 0.09; 'underlying': 0.09; 'backward': 0.16; 'benjamin': 0.16; 'clone': 0.16; 'copied.': 0.16; 'elements,': 0.16; 'instantiate': 0.16; 'integers,': 0.16; 'multiplied': 0.16; 'operation.': 0.16; 'reason.': 0.16; 'received:64.202.165.33': 0.16; 'received:smtpauth11.prod.mesa1.secureserver.net': 0.16; 'shallow': 0.16; 'wrote:': 0.17; 'element': 0.17; 'implementing': 0.17; 'typing': 0.17; '>>>': 0.18; 'otherwise,': 0.20; 'bit': 0.21; 'modifying': 0.22; 'subject:skip:i 10': 0.22; 'example': 0.23; 'programming': 0.23; 'elements': 0.23; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'common': 0.26; 'implemented': 0.27; 'plain': 0.27; 'reflect': 0.27; 'subject:list': 0.28; 'chris': 0.28; 'initial': 0.28; 'run': 0.28; 'post': 0.28; 'behaviour': 0.29; 'closer': 0.29; 'factor': 0.29; 'routine': 0.29; 'array': 0.29; "we're": 0.30; 'stuff': 0.30; 'lists': 0.31; 'point': 0.31; 'could': 0.32; 'problem': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'everyone': 0.33; "can't": 0.34; 'wrong': 0.34; 'list': 0.35; 'needed': 0.35; 'whatever': 0.35; 'lists.': 0.35; 'nov': 0.35; 'so,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'something': 0.35; 'list.': 0.35; 'really': 0.36; 'but': 0.36; "wasn't": 0.36; 'anything': 0.36; 'enough': 0.36; 'bad': 0.37; 'level': 0.37; 'being': 0.37; 'why': 0.37; 'quite': 0.37; 'rather': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'object': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'short': 0.39; 'where': 0.40; 'subject:-': 0.40; 'think': 0.40; 'claim': 0.60; 'easy': 0.60; 'most': 0.61; "you've": 0.61; 'received:network': 0.61; '(that': 0.62; 'relatively': 0.62; 'worth': 0.63; 'different': 0.63; 'received:unknown': 0.63; 'ever': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'within': 0.64; '10.': 0.64; 'behavior': 0.64; 'making': 0.64; 'copies.': 0.65; 'realise': 0.65; 'header:Reply-To:1': 0.68; 'further,': 0.71; 'reply-to:no real name:2**0': 0.72; 'low': 0.83; 'observed': 0.84; 'oscar': 0.84; 'mean.': 0.91; 'from.': 0.93 Date: Mon, 05 Nov 2012 21:51:24 -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> 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352181444 news.xs4all.nl 6925 [2001:888:2000:d::a6]:58935 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32806 On 11/05/2012 06:30 PM, Oscar Benjamin wrote: > On 6 November 2012 02:01, Chris Angelico wrote: >> On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin >> wrote: >>> I was just thinking to myself that it would be a hard thing to change >>> because the list would need to know how to instantiate copies of all >>> the different types of the elements in the list. Then I realised it >>> doesn't. It is simply a case of how the list multiplication operator >>> is implemented and whether it chooses to use a reference to the same >>> list or make a copy of that list. Since all of this is implemented >>> within the same list type it is a relatively easy change to make >>> (ignoring backward compatibility concerns). >>> >>> I don't see this non-copying list multiplication behaviour as >>> contradictory but has anyone ever actually found a use for it? >> Stupid example of why it can't copy: >> >> bad = [open("test_file")] * 4 >> >> How do you clone something that isn't Plain Old Data? Ultimately, >> that's where the problem comes from. It's easy enough to clone >> something that's all scalars (strings, integers, None, etc) and >> non-recursive lists/dicts of scalars, but anything more complicated >> than that is rather harder. > That's not what I meant. But now you've made me realise that I was > wrong about what I did mean. In the case of > > stuff = [[obj] * n] * m > > I thought that the multiplication of the inner list ([obj] * n) by m > could create a new list of lists using copies. On closer inspection I > see that the list being multiplied is in fact [[obj] * n] and that > this list can only know that it is a list of lists by inspecting its > element(s) which makes things more complicated. > > I retract my claim that this change would be easy to implement. > > > Oscar Hi Oscar, In general, people don't use element multiplication (that I have *ever* seen) to make lists where all elements of the outer most list point to the same sub-*list* by reference. The most common use of the multiplication is to fill an array with a constant, or short list of constants; Hence, almost everyone has to work around the issue as the initial poster did by using a much longer construction. The most compact notation in programming really ought to reflect the most *commonly* desired operation. Otherwise, we're really just making people do extra typing for no reason. Further, list comprehensions take quite a bit longer to run than low level copies; by a factor of roughly 10. SO, it really would be worth implementing the underlying logic -- even if it wasn't super easy. I really don't think doing a shallow copy of lists would break anyone's program. The non-list elements, whatever they are, can be left as reference copies -- but any element which is a list ought to be shallow copied. The behavior observed in the opening post where modifying one element of a sub-list, modifies all elements of all sub-lists is never desired as far as I have ever witnessed. The underlying implementation of Python can check an object type trivially, and the only routine needed is a shallow list copy. So, no it really isn't a complicated operation to do shallow copies of lists. :)