Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'assignment': 0.07; 'compile-time': 0.09; 'exec': 0.09; 'function,': 0.09; 'lookup': 0.09; 'namespace': 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; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; '"global"': 0.16; '###': 0.16; 'at,': 0.16; 'exist.': 0.16; 'expect,': 0.16; "function's": 0.16; 'globals.': 0.16; 'increment': 0.16; 'local.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'scope.': 0.16; 'url:faq': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'advance.': 0.19; '>>>': 0.22; 'programming': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'faq': 0.24; 'section.': 0.24; 'mon,': 0.24; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'skip:- 40': 0.29; 'am,': 0.29; "doesn't": 0.30; 'url:mailman': 0.30; 'code': 0.31; '"",': 0.31; 'ok.': 0.31; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; '(most': 0.33; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'anywhere': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'explains': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'similar': 0.36; 'url:org': 0.36; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'explain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'url:mail': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'most': 0.60; 'url:3': 0.61; 'kindly': 0.61; 'first': 0.61; 'provide': 0.64; 'different': 0.65; 'note:': 0.66; 'joel': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: A syntax question Date: Mon, 10 Nov 2014 16:10:17 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdbdf0.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 98 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415632232 news.xs4all.nl 2963 [2001:888:2000:d::a6]:42906 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80469 Joel Goldstick wrote: > On Mon, Nov 10, 2014 at 6:39 AM, Wolfgang Maier > wrote: >> You may want to read: >> >> https://docs.python.org/3/faq/programming.html?highlight=global#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value >> >> from the Python docs Programming FAQ section. >> It explains your problem pretty well. >> >> As others have hinted at, always provide concrete Python error messages >> and tracebacks instead of vague descriptions. >> >> Best, >> Wolfgang >> >> >> >> On 11/10/2014 12:07 PM, Mok-Kong Shen wrote: >>> >>> >>> I don't understand the following phenomenon. Could someone kindly >>> explain it? Thanks in advance. >>> >>> M. K. Shen >>> >>> ------------------------------------------------- >>> >>> count=5 >>> >>> def test(): >>> print(count) >>> if count==5: >>> count+=0 ### Error message if this line is active, otherwise ok. >>> print(count) >>> return >>> >>> test() >> >> >> -- >> https://mail.python.org/mailman/listinfo/python-list > > Your problem is that count is not local. You are reading count from > an outer scope. When you try to increment count in your function, it > can't because it doesn't exist. > Don't use globals. That's what most would expect, but the error is already triggered by the first print(count) Python decides at compile-time that count is a local variable if there is an assignment ("name binding") to count anywhere in the function's scope -- even if the corresponding code will never be executed: >>> x = 42 >>> def test(): ... print(x) ... if 0: x = 42 ... >>> test() Traceback (most recent call last): File "", line 1, in File "", line 2, in test UnboundLocalError: local variable 'x' referenced before assignment This is different from the class body where the global namespace is tried when a lookup in the local namespace fails: >>> x = 42 >>> class A: ... print(x) ... x += 1 ... 42 >>> x 42 >>> A.x 43 Historical ;) note: In Python 2 you could trigger a similar behaviour with exec: >>> def f(a): ... if a: exec "x = 42" ... print x ... >>> x = "global" >>> f(True) 42 >>> f(False) global