Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!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.056 X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'operator': 0.03; 'none,': 0.05; 'cc:addr:python-list': 0.10; 'backward': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'clone': 0.16; 'instantiate': 0.16; 'integers,': 0.16; 'multiplied': 0.16; 'wrote:': 0.17; 'subject:skip:i 10': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'elements': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'skip:[ 10': 0.26; 'implemented': 0.27; 'plain': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'chris': 0.28; 'behaviour': 0.29; 'closer': 0.29; 'received:209.85.215.46': 0.30; 'stuff': 0.30; 'lists': 0.31; 'could': 0.32; 'problem': 0.33; 'anyone': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'list': 0.35; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'list.': 0.35; 'but': 0.36; 'anything': 0.36; 'enough': 0.36; 'bad': 0.37; 'being': 0.37; 'why': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'things': 0.38; 'where': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'claim': 0.60; 'easy': 0.60; "you've": 0.61; 'relatively': 0.62; 'different': 0.63; 'ever': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'within': 0.64; 'copies.': 0.65; 'realise': 0.65; 'oscar': 0.84; 'mean.': 0.91; 'from.': 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 :cc:content-type; bh=7sNZuRleLkMPFa0m24YvHe8Vlg7vrq9g5I4kW1BaL8I=; b=TuK+aZ8qh/NY5O1cqwFGDDgmkia2QaAeWVuD+99wCRAF2BP6qPI3NASkaO5rv0n7kA 3TBMGQUcRLQCIKy1yx5Zws8MCPtVV5GXuoSTnEljFPtulW2MhQcF+dpFrZpZlPtY/Z0G zJkk4PgXl3EcBhsd8cfSYfs0o3kuJGSW6k0jkaV5lO6n4VkjnHPmBRGrJzfQohC+vNSs urSGpt/2RgHhFN88ye9c/yM8Vc7uuAjB1iMAXsvm+Z8vaXx6tuewJmt9Q4tUA8R0JTt+ 7hFiG5zB9qe7T0xvXx7fy4pojlxxYe/s4PHTyNQO8laQ42RdpT8n6E3tg51FETXWca2l j+Pg== MIME-Version: 1.0 In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> Date: Tue, 6 Nov 2012 02:30:00 +0000 Subject: Re: Multi-dimensional list initialization From: Oscar Benjamin To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352169003 news.xs4all.nl 6847 [2001:888:2000:d::a6]:34825 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32793 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