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!newsfeed6.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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'operator': 0.03; 'none,': 0.05; 'result,': 0.05; 'typeerror:': 0.09; '[2,': 0.16; 'backward': 0.16; 'benjamin': 0.16; 'clone': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instantiate': 0.16; 'integers,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'import': 0.21; 'received:209.85.214.174': 0.21; '"",': 0.22; 'default,': 0.22; 'subject:skip:i 10': 0.22; 'example': 0.23; 'elements': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'skip:" 20': 0.26; '(most': 0.27; 'implemented': 0.27; 'plain': 0.27; 'skip:b 30': 0.27; 'message- id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'behaviour': 0.29; 'skip:_ 10': 0.29; "skip:' 10": 0.30; 'file': 0.32; 'traceback': 0.33; 'handle': 0.33; 'problem': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'list': 0.35; 'nov': 0.35; 'doing': 0.35; 'pm,': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'list.': 0.35; 'but': 0.36; 'anything': 0.36; 'enough': 0.36; 'bad': 0.37; 'why': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'where': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'easy': 0.60; 'relatively': 0.62; 'skip:y 20': 0.62; 'safe': 0.63; 'different': 0.63; 'ever': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'within': 0.64; 'oscar': 0.84; '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 :content-type; bh=VF9eAJdOPe2BHfvESVyMz9PMPYxMBrNerIotCCJ9Uqg=; b=O4T45LwgSrqxGWd79IMROl7JMCU0o4YHyJF9OsdC31eivTmfugMW/0R6FG54Yw6FLh HRS0WajsBR2WNQ/1ARAz2YdvoGxPzlfKRUiR0+3V31jmscmh7G57MGKh2t8XRWydjwTT kG+e8J/OPRVk9HeFb/sMjWtD7k+xMk1Hou+sqBYzvEddUbrAuAkx/QP3gM5QlWCq7JqQ 4SmWj07IEaPWAeTO32eeOFu6jM270Prgdph7UTf36GL5GrVnXVMvOgyr+maX4bYyeHZE R9kO1+ubkhI/zq1mozn7rvNwUJl/c5fGj2BZRjfsiRAwDvd1oDFiERY1TRmilXCJl5BY XVXw== MIME-Version: 1.0 In-Reply-To: References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> Date: Tue, 6 Nov 2012 13:01:46 +1100 Subject: Re: Multi-dimensional list initialization 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352167309 news.xs4all.nl 6858 [2001:888:2000:d::a6]:56651 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32792 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. If you want a deep copy and are prepared to handle any issues that might result, you can do this: >>> import copy >>> a=[[2,3,4]] >>> a.extend(copy.deepcopy(a)) >>> a[0][1]=10 >>> a [[2, 10, 4], [2, 3, 4]] And some things just won't work: >>> bad.extend(copy.deepcopy(bad)) Traceback (most recent call last): File "", line 1, in bad.extend(copy.deepcopy(bad)) File "C:\Python32\lib\copy.py", line 147, in deepcopy y = copier(x, memo) File "C:\Python32\lib\copy.py", line 209, in _deepcopy_list y.append(deepcopy(a, memo)) File "C:\Python32\lib\copy.py", line 166, in deepcopy rv = reductor(2) TypeError: cannot serialize '_io.TextIOWrapper' object The default behaviour is safe and reliable. When you want something other than the default, there are ways of doing it. ChrisA