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!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; 'received:64.202.165': 0.07; 'semantic': 0.07; 'python': 0.09; 'compact': 0.09; 'logic': 0.09; 'modifies': 0.09; 'notation': 0.09; 'subtle': 0.09; 'underlying': 0.09; 'cc:addr:python-list': 0.10; 'behaviour.': 0.16; 'benjamin': 0.16; 'bit.': 0.16; 'copied.': 0.16; 'elements,': 0.16; 'gotcha.': 0.16; 'operation.': 0.16; 'reason.': 0.16; 'shallow': 0.16; 'subclasses,': 0.16; 'subclassing': 0.16; 'surprising': 0.16; 'wrote:': 0.17; 'element': 0.17; 'implementing': 0.17; 'typing': 0.17; 'otherwise,': 0.20; 'bit': 0.21; 'modifying': 0.22; 'subject:skip:i 10': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'programming': 0.23; 'elements': 0.23; 'for?': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'so.': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'common': 0.26; 'am,': 0.27; 'first.': 0.27; 'reflect': 0.27; 'this?': 0.28; 'subject:list': 0.28; 'initial': 0.28; 'run': 0.28; 'post': 0.28; 'behaviour': 0.29; 'factor': 0.29; 'methods.': 0.29; 'routine': 0.29; 'array': 0.29; "we're": 0.30; 'expect': 0.31; 'lists': 0.31; 'point': 0.31; 'problem.': 0.32; "aren't": 0.33; 'event.': 0.33; 'point,': 0.33; 'everyone': 0.33; 'that,': 0.34; 'list': 0.35; 'needed': 0.35; 'whatever': 0.35; 'lists.': 0.35; 'so,': 0.35; 'doing': 0.35; 'something': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'wanted': 0.36; "wasn't": 0.36; 'method': 0.36; "i'll": 0.36; 'test': 0.36; 'should': 0.36; 'level': 0.37; 'quite': 0.37; 'far': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'object': 0.38; 'things': 0.38; 'short': 0.39; 'where': 0.40; 'subject:-': 0.40; 'think': 0.40; 'most': 0.61; 'received:network': 0.61; '(that': 0.62; 'worth': 0.63; 'different': 0.63; 'received:unknown': 0.63; 'ever': 0.63; '10.': 0.64; 'behavior': 0.64; 'making': 0.64; 'header:Reply-To:1': 0.68; 'further,': 0.71; 'reply-to:no real name:2**0': 0.72; 'low': 0.83; 'imagine,': 0.84; 'observed': 0.84; 'oscar': 0.84 Date: Tue, 06 Nov 2012 13:14:10 -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: Oscar Benjamin 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 Cc: python-list@python.org 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352236812 news.xs4all.nl 6983 [2001:888:2000:d::a6]:53241 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32842 On 11/06/2012 06:35 AM, Oscar Benjamin wrote: > > > 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. > > That's what I have seen as well. I've never seen an example where > someone wanted this behaviour. > > > > > 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. > > It's not so much the typing as the fact that this a common gotcha. > Apparently many people expect different behaviour here. I seem to > remember finding this surprising at first. > :) That's true as well. > > > > > 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. > > It is a semantic change that would, I imagine, break many things in > subtle ways. > ?? Do you have any guesses, how ? > > > > > 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. > > Yes but if you're inspecting the object to find out whether to copy it > what do you test for? If you check for a list type what about > subclasses? What if someone else has a custom list type that is not a > subclass? Should there be a dunder method for this? > No dunder methods. :) Custom non-subclass list types aren't a common usage for list multiplication in any event. At present one has to do list comprehensions for that, and that would simply remain so. Subclasses, however, are something I hadn't considered... > I don't think it's such a simple problem. > > Oscar > You made a good point, Oscar; I'll have to think about the subclassing a bit. :)