Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'example:': 0.03; 'assignment': 0.07; 'compiler': 0.07; 'variables': 0.07; "'a'": 0.09; 'function,': 0.09; 'namespace': 0.09; 'referenced': 0.09; 'python': 0.11; 'def': 0.12; "'b'": 0.16; 'assignment.': 0.16; 'assignments.': 0.16; 'bind': 0.16; 'confuse': 0.16; 'dictionary,': 0.16; 'immutable,': 0.16; 'modified.': 0.16; 'statement.': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'normally': 0.19; 'seems': 0.21; 'example': 0.22; 'header:User-Agent:1': 0.23; 'question': 0.24; 'sort': 0.25; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; '>>>>': 0.31; 'names.': 0.31; 'object.': 0.31; 'occurs': 0.31; 'subject:About': 0.31; 'another': 0.32; 'checking': 0.33; 'not.': 0.33; 'could': 0.34; 'objects': 0.35; 'but': 0.35; 'functions.': 0.36; 'module.': 0.36; 'list.': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'name': 0.63; 'within': 0.65; 'received:74.208': 0.68; 'toy': 0.84 Date: Thu, 04 Dec 2014 18:54:26 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: About Modifying Globals References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:54MmgZDWae2KZcK1lsNRp2QABE+7SZ1glka+f9BQMQN Owss6i268OvzTFnOM8bggwGJm5S7hMK8x6QenOI9cocVkr0XQP xiwn8vveZnpDgebmUT1aXD8vYk7e05mswTFullKKW8jmNoQg5J v/AhPXUeCdjYVgcU4Gtnhybkm+wKIjZXMoZUkuUDw2uYjK6e2j Y6VM3QFhkOJhwrIOguAEsPn52TwTcrme9zBntC71ZTuhlzNcnz 0Tv4vLEO4C6L4rkCSUKHuKYAuN/UDXOfI4RbSwxaUDG6fZr3Uu xqeDbWgTtdpxWS1eK5TKzjWgrNOrqaPgITKoFm3Lv+836JXuq4 aWwgO7XCiL7ud1c+prII= X-UI-Out-Filterresults: notjunk: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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417737288 news.xs4all.nl 2923 [2001:888:2000:d::a6]:40556 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81962 On 12/04/2014 03:09 PM, LJ wrote: > Hi All, > > I have a quick question regarding the modification of global variables within functions. To illustrate, consider the following toy example: > > a={"1": set()} > b=9 > > def gt(l): > a["1"] = a["1"] | set([l]) > > When calling this last function and checking the a dictionary, I get: > >>>> gt(5) >>>> a > {"1": set([5])} > > > The set in the dictionary was modified. The question is, why isn't it necessary to declare a as global within the gt function, as apposed to a case like > > def gt2(l): > b=b+l > > where I need to declare b as global within the function to avoid: > > UnboundLocalError: local variable 'b' referenced before assignment. > The reason this sort of thing seems to confuse lots of people is that we insist on calling these things assignments. The thing that's global is the name 'a'. It's global because it's in the global() namespace of some module. The data that's "assigned" to it is an object. You bind the object to the name 'a' with an assignment statement. If that object is immutable, then the only way to change 'a' is to do another assignment. But if the object is mutable, as a set is, then it can change as much as you like without rebinding the name. A given object may have any number of names bound to it, including no names. An example of that could be objects that are referenced only in some list. Python doesn't have declarations, so when a function is compiled, the compiler has to infer what names are to be local and what are not. The rule it normally uses is roughly based on whether an assignment occurs somewhere inside the function. -- DaveA