Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"this': 0.03; 'argument': 0.05; 'say,': 0.05; 'subject:Python': 0.06; 'assign': 0.07; 'assignment': 0.07; 'assigning': 0.09; 'happens.': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'barrier': 0.16; 'declaration': 0.16; 'fits': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'magic': 0.16; 'picks': 0.16; 'roy': 0.16; 'special.': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'thu,': 0.19; 'work,': 0.20; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'sort': 0.25; 'handling': 0.26; 'header:In- Reply-To:1': 0.27; 'mode': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'probably': 0.32; 'stuff': 0.32; 'run': 0.32; 'becomes': 0.33; 'totally': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'reality': 0.36; 'method': 0.36; 'easiest': 0.38; 'pm,': 0.38; 'that,': 0.38; 'anything': 0.39; 'explain': 0.39; 'enough': 0.39; 'called': 0.40; "you're": 0.61; 'further': 0.61; 'name': 0.63; 'kind': 0.63; 'subject:The': 0.64; 'different': 0.65; 'between': 0.67; 'smith': 0.68; 'subjectcharset:utf-8': 0.72; 'subject:have': 0.80; 'compare:': 0.84; "it'd": 0.84; 'write:': 0.91; 'to:none': 0.92 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:cc :content-type; bh=9wtMcVt2hS+MAAGAiTgnOVxoR+6MAp72RluPqPNsnyU=; b=DgUhbLWOpKMroZSUedT8a1KJ2UOo52KAUm9kl8cgBYrLdVCWhUHaCQm6qP6EG3x8EP xly1tFG34RWQ2JD3LMYm8Qj6XYQIRoAePYwzoqyzrMm8QdO8H8CvqjxL8peOjP1r75aK Yilh01o036uGl/ZI1Gkv6P3aqvYc3ebPJdyAJf6sYyvJhsRYLeaxTZ9NGJg0fy38oMur +Fdgcg7vKAsSCjghzXgPPIXLKvPBPmIRvuWnXIAvvwreqvZHycMjtFWDsYeNNjGDhehS 7wws3TCFOmoqnlGLummqEzWivg4ZxV8n3MUfaCmiUceZ15ztcLyVR1424p+504Y4GPmY AQtA== MIME-Version: 1.0 X-Received: by 10.58.185.145 with SMTP id fc17mr2757507vec.14.1399553664225; Thu, 08 May 2014 05:54:24 -0700 (PDT) In-Reply-To: References: <235C4BFA-9770-481A-9FCF-21C3F036769C@gmail.com> <5368681D.8070602@islandtraining.com> <85zjiuea37.fsf_-_@benfinney.id.au> <8738gmxgay.fsf@elektro.pacujo.net> Date: Thu, 8 May 2014 22:54:24 +1000 Subject: =?UTF-8?B?UmU6IFRoZSDigJxkb2VzIFB5dGhvbiBoYXZlIHZhcmlhYmxlcz/igJ0gZGViYXRl?= From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399553673 news.xs4all.nl 2888 [2001:888:2000:d::a6]:55912 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71094 On Thu, May 8, 2014 at 10:41 PM, Roy Smith wrote: > At some point, that model no longer fits reality well enough that it > becomes a barrier to further learning. When I write: > > def mutate(x, y): > x = 42 > y[0] = 42 > > x = 4 > y = [4] > > mutate(x, y) > print x, y > > and run it, unless I really understand about name binding and argument > passing, I'm going to be totally befuddled by the result. Easiest way to explain that is probably to just say, right at the beginning, that *assignment to a name is special*. When you assign to x inside mutate(), stuff happens. You're not assigning to y, so different stuff happens. If you called a method on y, or anything like that, it'd be the same kind of "stuff" as you're seeing here; but assigning directly to the name is a different sort of operation from everything else. (Explaining the difference between "x = x + 1" and "x += 1" can come later. MUCH later, probably when you try to augassign to a tuple's element.) The advantage of singling out assignment is that, as well as handling the difference between rebinding and mutation, it also picks up the magic "this is now local" mode change. Compare: x = [1,2,3] def mutate1(): x += [4] def mutate2(): x.append(4) One of them needs a 'global' declaration to work, and the other doesn't, because assigning is special. ChrisA