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


Groups > comp.lang.python > #65059

Re: Why this throws an UnboundLocalError ?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'value,': 0.04; 'explicitly': 0.05; 'assign': 0.07; 'subject:Why': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'assume': 0.14; 'posted': 0.15; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'obviously,': 0.16; 'which,': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; '31,': 0.24; 'example.': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'constant': 0.31; 'marc': 0.31; 'fri,': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'changing': 0.37; 'example,': 0.37; 'thank': 0.38; 'bad': 0.39; 'called': 0.40; 'tell': 0.60; 'full': 0.61; "you're": 0.61; 'you.': 0.62; 'complete': 0.62; 'name': 0.63; 'such': 0.63; 'subject:this': 0.83; 'local,': 0.84; 'cutting': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=67pn/AtS6nnqQGmP4SJvuK1sebTAJUm9sNc6nKaVPeA=; b=tF8pD1+/0xnFtRmlOSq2tarK22+JWSyOLv6o5qMPUYxgdCmPGnss58povGrY2Wzj1K fFCQ1dbOStegrR8jFfT2MyJPkTgEOGRgTuVqmdsDgSb4Fe4CX7dNoowPesLywboGxFac Fsyh1XklV9QykA7NIs4eVHEzDS3n6skyxRK5TsKHrLJg3BzzlUng/XpihTCMRL3R4+87 BgwUXBolHn4TZVB3gvhWEHdMiKNUZxScpnCTtQyKD4AmtJpvNqntebuCdkfZ8ZsZNtn/ HYvgtluKgsA2tt6VKYysRLUNtPBR+kcvR0Y/MO+AO0JTo7ytS2WPxgmzq5IKXnroNm1L K1kA==
MIME-Version 1.0
X-Received by 10.66.129.133 with SMTP id nw5mr17047303pab.98.1391122398309; Thu, 30 Jan 2014 14:53:18 -0800 (PST)
In-Reply-To <CA+DCN_tQsZDckkueO7FiVYKihy2b9=SdeALJ4RwrQN_uk0dDLQ@mail.gmail.com>
References <CA+DCN_tQsZDckkueO7FiVYKihy2b9=SdeALJ4RwrQN_uk0dDLQ@mail.gmail.com>
Date Fri, 31 Jan 2014 09:53:18 +1100
Subject Re: Why this throws an UnboundLocalError ?
From Chris Angelico <rosuav@gmail.com>
Cc "comp.lang.python" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6173.1391122402.18130.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391122402 news.xs4all.nl 2869 [2001:888:2000:d::a6]:47615
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65059

Show key headers only | View raw


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!!)

ChrisA

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


Thread

Re: Why this throws an UnboundLocalError ? Chris Angelico <rosuav@gmail.com> - 2014-01-31 09:53 +1100

csiph-web