Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80469

Re: A syntax question

From Peter Otten <__peter__@web.de>
Subject Re: A syntax question
Date 2014-11-10 16:10 +0100
Organization None
References <m3q6ae$m8p$1@news.albasani.net> <m3q84d$6is$1@ger.gmane.org> <CAPM-O+zDYxr-EVJtiC92R-VfGvr33ByZ1jkVdkEPdgbv0CVdog@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.15625.1415632232.18130.python-list@python.org> (permalink)

Show all headers | View raw


Joel Goldstick wrote:

> On Mon, Nov 10, 2014 at 6:39 AM, Wolfgang Maier
> <wolfgang.maier@biologie.uni-freiburg.de> 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 "<stdin>", line 1, in <module>
  File "<stdin>", 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


Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A syntax question Mok-Kong Shen <mok-kong.shen@t-online.de> - 2014-11-10 12:07 +0100
  Re: A syntax question alister <alister.nospam.ware@ntlworld.com> - 2014-11-10 11:23 +0000
    Re: A syntax question David Palao <dpalao.python@gmail.com> - 2014-11-10 12:31 +0100
      Re: A syntax question Grant Edwards <invalid@invalid.invalid> - 2014-11-10 14:44 +0000
        Re: A syntax question alister <alister.nospam.ware@ntlworld.com> - 2014-11-10 14:54 +0000
          Re: A syntax question alister <alister.nospam.ware@ntlworld.com> - 2014-11-10 14:56 +0000
            Re: A syntax question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-10 19:53 -0500
              Re: A syntax question alister <alister.nospam.ware@ntlworld.com> - 2014-11-11 09:04 +0000
          Re: A syntax question Grant Edwards <invalid@invalid.invalid> - 2014-11-10 16:11 +0000
            Re: A syntax question Chris Angelico <rosuav@gmail.com> - 2014-11-11 03:18 +1100
              Re: A syntax question Roy Smith <roy@panix.com> - 2014-11-10 14:48 -0500
    Re: A syntax question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-10 20:08 +0000
    Re: A syntax question Tim Chase <python.list@tim.thechases.com> - 2014-11-10 14:29 -0600
  Re: A syntax question Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-11-10 12:39 +0100
  Re: A syntax question Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-10 09:35 -0500
  Re: A syntax question Chris Angelico <rosuav@gmail.com> - 2014-11-11 01:54 +1100
  Re: A syntax question Peter Otten <__peter__@web.de> - 2014-11-10 16:10 +0100
  Re: A syntax question Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-10 10:27 -0500

csiph-web