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


Groups > comp.lang.python > #33340

Re: Error

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'error:': 0.05; 'fixes': 0.05; 'initialize': 0.05; 'assign': 0.07; 'executed': 0.07; 'function,': 0.07; 'subject:Error': 0.07; 'logic': 0.09; 'referenced': 0.09; 'def': 0.10; "hasn't": 0.15; 'dist': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'iteration': 0.16; 'value;': 0.16; 'variable.': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'code.': 0.20; 'variable': 0.20; 'do.': 0.21; 'assignment': 0.22; '15,': 0.23; 'sets': 0.23; "haven't": 0.23; 'somewhere': 0.24; 'header:In- Reply-To:1': 0.25; 'looks': 0.26; 'am,': 0.27; 'possibly': 0.27; 'set.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'decide': 0.28; 'implicitly': 0.29; 'loop,': 0.29; 'error': 0.30; 'figure': 0.30; 'code': 0.31; 'point': 0.31; 'gets': 0.32; 'help,': 0.32; 'getting': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'point.': 0.33; 'that,': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'nov': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'should': 0.36; 'unable': 0.36; 'possible': 0.37; 'beyond': 0.37; 'execute': 0.37; 'why': 0.37; 'previous': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'you.': 0.61; 'first': 0.61; "you'll": 0.62; 'distance': 0.62; 'here': 0.65; 'hand': 0.82; 'brief,': 0.84
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:to :content-type; bh=9BDDUejb9o46Fg4GtxFGFrLfYdGqy1DNSF1OWU5sGDg=; b=R0Ai6axT7COUjcR8goQGVQ4t0l9e6BtU/HAS6yBHtF/nlxnMNF28qZl3Yw4MFJrr90 PocoUJhXBEQXWJuoQW0au38WhETgjnby6xCL02YNxYzyDlt8ofjx2peJwnVhZ+Bw7yvo GTnZhZAJZnUtdJbj4xxdb/gYQVR9/ThFHn1Vq44ECK7e4AT5IHwY/hRVS5WE5hrae6ty SK4TIo9RqKVFPWi3MyDWDqQgg2cAzuVTNM62Mwe1Cz0f4xfqa0zgXzN4TEAp07t6ydum rdOX5BmAbg8KZciFiVmKbIv8c3/EH9D7S6rQkqJfZP7mvYm9VR24DTvNkmoZevKKEf9K nQGg==
MIME-Version 1.0
In-Reply-To <CAFqGZRGYDM_3H657gid9GUxrSbnM_92nCZEsWWcWhFua5xEpmw@mail.gmail.com>
References <CAFqGZRGYDM_3H657gid9GUxrSbnM_92nCZEsWWcWhFua5xEpmw@mail.gmail.com>
Date Thu, 15 Nov 2012 02:28:12 +1100
Subject Re: Error
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3686.1352906895.27098.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352906895 news.xs4all.nl 6849 [2001:888:2000:d::a6]:43082
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33340

Show key headers only | View raw


On Thu, Nov 15, 2012 at 2:18 AM, inshu chauhan <insideshoes@gmail.com> wrote:
>
> for this code m getting this error :
>
> CODE :
> def ComputeClasses(data):
>             if data[cy,cx] != (0.0,0.0,0.0):
>                 centre = data[cy, cx]
> ...
>                             dist = distance(centre, point)
>
> ERROR :
> UnboundLocalError: local variable 'centre' referenced before assignment
>
> And i am unable to understand .. WHY ?

In brief, here's what causes that error:

1) Somewhere in the function, you assign to that name, which
implicitly sets it to be a local variable. That happens there where
you go "centre = data[cy, cx]".

2) Somewhere else in the function, you reference that name. That
happens where you try to calculate the distance from your
previously-defined centre to the current point.

3) At run-time, you haven't executed #1, but you do execute #2.

Your problem here I can't diagnose, but it looks like your first point
is (0.0,0.0,0.0), so centre never gets set. There are a couple of
possible fixes for this, and you'll need to figure out what to do
based on knowing your own code. Possibly you just need to initialize
centre above the loop, so that it always has a valid value; or
possibly the code below needs to not execute if the current centre
hasn't been set.

Go through your function's logic by hand and figure out what happens
when, and whether that's what it ought to do. Then decide what should
happen when a data value is (0.0,0.0,0.0) - currently it's retaining
the value of centre from the previous iteration of the loop, which
smells wrong to me.

Beyond that, I don't think I can really help, it's up to you.

ChrisA

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


Thread

Re: Error Chris Angelico <rosuav@gmail.com> - 2012-11-15 02:28 +1100

csiph-web