Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'element': 0.07; 'assigning': 0.09; 'function,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"a"': 0.16; '"b"': 0.16; 'assigns': 0.16; 'change;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'referencing': 0.16; 'subscripted': 0.16; 'wrote:': 0.18; 'replacing': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'object.': 0.31; 'second,': 0.31; 'subject:About': 0.31; 'fri,': 0.33; 'actual': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'two': 0.37; 'new': 0.61; "you're": 0.61; 'first': 0.61; 'name': 0.63; '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=qv9nOirBhm3sV70WIhwtdL33EDY7XtUk6HeJt2JII8k=; b=YaJUX+vcsqx8GZQokiwZkefveN6zkGzFue4bacUkOP7PyqJCskcvQRCl1Vu8HPOI9X 3zaHTOLx6h/6plPQ3GoEfyDiqw9AbSXVEJeWChx/ulKLyI1s3gHNu6/eOJs/Ncdd4S2H 0n+vvitiksZit0K8VdohRqMDOV29UQbKUmZiAxQaOgbIZAsp9OcbhjOSsH2W0buoj5C5 qgHLD0+JHovUnqsyWD5xunR61V4+OR7Tk+S9L+ph1AFivx/9wedJBhGxbvZXrpoVIVG4 /tcMppm78x3H0yHsKBbZ9Jc9RVCpj9pawrsi4y05jmOiRo+Xn2/SZIZnQq4w8WjHenwn 9D6w== MIME-Version: 1.0 X-Received: by 10.42.130.194 with SMTP id w2mr13064100ics.12.1417724535567; Thu, 04 Dec 2014 12:22:15 -0800 (PST) In-Reply-To: References: Date: Fri, 5 Dec 2014 07:22:15 +1100 Subject: Re: About Modifying Globals 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: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417724539 news.xs4all.nl 2902 [2001:888:2000:d::a6]:38383 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81943 On Fri, Dec 5, 2014 at 7:09 AM, LJ wrote: > def gt(l): > a["1"] = a["1"] | set([l]) > > > def gt2(l): > b=b+l These two may both look like they're assigning something, but one of them is assigning directly to the name "b", while the other assigns to a subscripted element of "a". In the first function, "a" is still the same object after the change; in the second, "b" has been replaced by a new object. That's why your second one needs a global declaration. It's all about names and assignment; if you're not assigning to the actual name, there's no need to declare where the name comes from, because you're only referencing it, not replacing it. ChrisA