Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'assign': 0.07; 'subject:code': 0.07; 'references.': 0.09; 'referencing': 0.09; 'unpacking': 0.09; '12:50': 0.16; '24,': 0.16; '[none,': 0.16; 'bind': 0.16; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterable': 0.16; 'none]': 0.16; 'silly': 0.16; 'substitute': 0.16; 'wrote:': 0.17; "shouldn't": 0.17; 'variable': 0.20; 'bit': 0.21; 'received:209.85.214.174': 0.21; 'this:': 0.23; 'header:In- Reply-To:1': 0.25; 'am,': 0.27; 'rules': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'objects': 0.29; "i'm": 0.29; 'function': 0.30; 'print': 0.32; 'to:addr:python- list': 0.33; 'code:': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'done': 0.34; 'clear': 0.35; 'same.': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'totally': 0.36; 'subject:with': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'list,': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; "you've": 0.61; 'love': 0.63; 'taking': 0.65; 'contents.': 0.65; 'jul': 0.65; 'gain': 0.79; 'subject:this': 0.84; 'magical': 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=XMJOEaqkSkFkf7KH3I1cU/2DoW9sjrE+pOeAHQovDAY=; b=Du3JPW7q6sIfXiy5/FpzLbc+Br8xVKMLZJjpChV18FLF9kNKROx0YvcCbhpB5cnVyN htPmI8bkM5hUZKjwGrW0Voq69t3s3uTfyONx/sP/JvIllHnsnDhqcaPDyHBh42scf14C LqBKbrHOK3OHusQHHpTVoiQj/Y7/Zuc4q9DlSbAwoaEllcMEW2xHL+OuQYrYH/TLN2zH YYTp1LxLyhd+oRk8PMO6PTWn42YT/8iZs0LJBhu2x0e4u4BOF571R739ctU0flAVhrEl ZYUmkisP5Lne9mNp2KcirhGkdLEyW9UOb/5j5hYh4ZqCEKZotOhYx7Y/bg2Cz6U/ssop sx7A== MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 24 Jul 2012 01:24:29 +1000 Subject: Re: What's wrong with this code? 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.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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343057076 news.xs4all.nl 6924 [2001:888:2000:d::a6]:44691 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25889 On Tue, Jul 24, 2012 at 12:50 AM, Stone Li wrote: > > I'm totally confused by this code: > > Code: Boiling it down to just the bit that matters: c = None d = None x = [c,d] e,f = x c = 1 d = 2 print e,f When you assign "e,f = x", you're taking the iterable x and unpacking its contents. There's no magical "referenceness" that makes e bind to the same thing as c; all that happens is that the objects in x gain additional references. When you rebind c and d later, that doesn't change x, nor e/f. What you've done is just this: x = [None, None] e,f = x c = 1 d = 2 print e,f It's clear from this version that changing c and d shouldn't have any effect on e and f. In Python, any time you use a named variable in an expression, you can substitute the object that that name is referencing - it's exactly the same. (That's one of the things I love about Python. No silly rules about what you can do with a function return value - if you have a function that returns a list, you can directly subscript or slice it. Yay!) ChrisA