Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!feed.xsnews.nl!border-2.ams.xsnews.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'defines': 0.07; 'elements.': 0.07; 'initialize': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'tuple': 0.09; '"b"': 0.16; '(empty': 0.16; ">it's": 0.16; '>this': 0.16; 'a,b,c': 0.16; 'array)': 0.16; 'arrays,': 0.16; 'arrays.': 0.16; 'bieber': 0.16; 'concepts.': 0.16; 'email addr:ix.netcom.com': 0.16; 'email name:wlfraed': 0.16; 'from:addr:ix.netcom.com': 0.16; 'from:addr:wlfraed': 0.16; 'from:name:dennis lee bieber': 0.16; 'message-id:@4ax.com': 0.16; 'received:wlfraed': 0.16; 'subject:=': 0.16; 'unpack': 0.16; 'url:netcom': 0.16; 'url:wlfraed': 0.16; 'wulfraed': 0.16; '(i.e.': 0.17; 'wed,': 0.17; 'language': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'seems': 0.20; 'url:home': 0.21; 'dec': 0.22; 'figure': 0.23; 'pile': 0.23; 'received:166': 0.23; 'lee': 0.28; 'lists': 0.28; 'skip:b 20': 0.29; 'array': 0.30; 'list).': 0.30; 'object.': 0.30; 'remains': 0.30; "didn't": 0.31; 'does': 0.32; 'list': 0.32; 'idea': 0.32; 'header:X-Complaints-To:1': 0.33; 'there': 0.33; 'object': 0.33; 'to:addr:python-list': 0.34; 'someone': 0.34; 'eric': 0.34; 'integer': 0.34; 'trouble': 0.35; 'connection': 0.36; 'example,': 0.37; 'charset:us-ascii': 0.37; 'but': 0.37; 'reference': 0.37; 'list,': 0.37; 'connects': 0.38; 'received:org': 0.38; 'some': 0.38; 'created': 0.38; 'easier': 0.38; 'goes': 0.39; 'define': 0.39; 'extend': 0.39; 'why': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; '2011': 0.61; 'your': 0.61; 'straight': 0.63; 'dennis': 0.73; '-0800': 0.84; 'disconnects': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: what does 'a=b=c=[]' do Date: Wed, 21 Dec 2011 18:20:16 -0500 References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: mobile-166-147-102-030.mycingular.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324509636 news.xs4all.nl 6939 [2001:888:2000:d::a6]:44084 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17708 On Wed, 21 Dec 2011 14:25:17 -0800 (PST), Eric wrote: >Except it seems that I didn't create three different arrays, I created >one array that goes by three different names (i.e. x[], y[] and z[] >all reference the same pile of numbers, no idea which pile). > >This surprises me, can someone tell me why it shouldn't? I figure if >I want to create and initialize three scalars the just do "a=b=c=7", >for example, so why not extend it to arrays. Also, is there a more >pythonic way to do "x=[], y=[], z=[]"? > >It's a slick language but I still have trouble wrapping my brain >around some of the concepts. > The key one is that lists ([] defines a list, not an array) are "mutable". Your "7" is not mutable. a = b = c = 7 does the same thing as a = b = c = [] which is to define the names "a", "b", and "c", and connects the three names to the single object (integer 7 or new empty list). b = 8 then /disconnects/ the name "b" from the object (empty list or integer 7) and connects the name to an integer 8 object. b = [1, 2, 3] disconnects the name "b" from the object and connects it to a list object containing three elements. b.append("something") however is going "inside" the object to change what it contains -- the connection remains the same object however. >>> a,b,c=[[] for x in range(3)] >>> a.append(1) >>> b [] >>> c [] >>> a [1] >>> For the amount of typing, it's easier to just do a straight line tuple unpack >>> a,b,c = ([],[],[]) >>> a.append(2) >>> a [2] >>> b [] >>> c [] >>> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/