Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'assign': 0.07; 'assignment': 0.07; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referenced': 0.09; 'statements': 0.09; 'subject:Why': 0.09; 'python': 0.11; 'def': 0.12; '2.7.3': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'module': 0.19; 'import': 0.22; 'aug': 0.22; 'header:User- Agent:1': 0.23; 'received:comcast.net': 0.24; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; '"",': 0.31; '>>>>': 0.31; 'marc': 0.31; 'names.': 0.31; 'anyone': 0.31; 'file': 0.32; '(most': 0.33; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'new': 0.61; 'simple': 0.61; 'name': 0.63; 'more': 0.64; 'dear': 0.65; 'subject:this': 0.83 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: Why this throws an UnboundLocalError ? Date: Thu, 30 Jan 2014 17:56:17 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391122593 news.xs4all.nl 2835 [2001:888:2000:d::a6]:52573 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65062 On 1/30/14 5:46 PM, Marc Aymerich wrote: > Dear all, > > I have a very simple module > > glic3@e4200:# cat globalstate.py > GLOBAL = 0 > > def update(): > GLOBAL += 1 > > > however it doesn't work!! > > glic3@e4200:# python > Python 2.7.3 (default, Aug 1 2012, 05:14:39) > [GCC 4.6.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import globalstate >>>> globalstate.update() > Traceback (most recent call last): > File "", line 1, in > File "globalstate.py", line 4, in update > GLOBAL += 1 > UnboundLocalError: local variable 'GLOBAL' referenced before assignment > > > And I don't know why :( > Anyone ? > > Thanks!! > Assignment statements in functions implicitly make local names. If you want to assign a new value to a global name in a function, you have to use a global statement: def update(): global GLOBAL GLOBAL += 1 -- Ned Batchelder, http://nedbatchelder.com