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


Groups > comp.lang.python > #65114

Re: Why this throws an UnboundLocalError ?

References <CA+DCN_tQsZDckkueO7FiVYKihy2b9=SdeALJ4RwrQN_uk0dDLQ@mail.gmail.com> <CAPTjJmrVNgbwLMZ9yiv1qzVgexU5amMrNf3LM+NVAy_-qr5qQQ@mail.gmail.com>
From Marc Aymerich <glicerinu@gmail.com>
Date 2014-01-31 12:05 +0100
Subject Re: Why this throws an UnboundLocalError ?
Newsgroups comp.lang.python
Message-ID <mailman.6203.1391166346.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Jan 30, 2014 at 11:53 PM, Chris Angelico <rosuav@gmail.com> wrote:
> On Fri, Jan 31, 2014 at 9:46 AM, Marc Aymerich <glicerinu@gmail.com> wrote:
>> GLOBAL = 0
>>
>> def update():
>>     GLOBAL += 1
>
> If you assign to a name, Python makes it local, unless you explicitly
> tell it that you want it to be global:
>
> def update():
>     global GLOBAL
>     GLOBAL += 1
>
> But be aware that the ALL_CAPS name conventionally means a constant.
> Since you're changing its value, it's not constant (wow! :) ), so
> using a name of GLOBAL is a bad idea. (Also, obviously, you want to
> name it appropriately to what you're doing, but I assume you called it
> this as part of cutting down your example. For which, by the way,
> thank you. You posted a complete example, and the full traceback, and
> the Python version and platform. That's everything that we need to
> help you - it's such a luxury!!)
>

Thank you very much guys!
and for the additional tips Chris. :)

I can't believe, all these years using Python and never encountered a
situation where I needed to use global !

-- 
Marc

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


Thread

Re: Why this throws an UnboundLocalError ? Marc Aymerich <glicerinu@gmail.com> - 2014-01-31 12:05 +0100

csiph-web