Sign in to follow this  
shanenin

Unboundlocalerror Python

Recommended Posts

I am getting this error with this finction

UnboundLocalError: local variable 'query_info' referenced before assignment

this is the function that is failing. This is just part of the code. the varibale query_info is set gloablly in the beginning of the script

def cd_info():

   if query_status == 200:
       (read_status, read_info) = CDDB.read(query_info['category'], query_info['disc_id'])
       song_list = []
       for i in range(disc_id[1]):
           song_list.append(read_info['TTITLE' + `i`])
       title = query_info['title']
       song_titles = tuple(song_list)
       return (title, song_titles)

   elif query_status in (210 ,211):
       query_info = query_info[0]
       (read_status, read_info) = CDDB.read(query_info['category'], query_info['disc_id'])
       song_list =[]
       for i in range(disc_id[1]):
           song_list.append(read_info['TTITLE' + `i`])
       title = query_info['title']
       song_titles = tuple(song_list)
       return (title, song_titles)
cd_info()

but if I use the same code not in a function, it works fine

if query_status == 200:
   (read_status, read_info) = CDDB.read(query_info['category'], query_info['disc_id'])
   song_list = []
   for i in range(disc_id[1]):
       song_list.append(read_info['TTITLE' + `i`])
   title = query_info['title']
   song_titles = tuple(song_list)
   
elif query_status in (210 ,211):
   query_info = query_info[0]
   (read_status, read_info) = CDDB.read(query_info['category'], query_info['disc_id'])
   song_list = []
   for i in range(disc_id[1]):
       song_list.append(read_info['TTITLE' + `i`])
   title = query_info['title']
   song_titles = tuple(song_list)

why do I get the error when using the code in the form of a function?

Share this post


Link to post
Share on other sites
        query_info = query_info[0]

<{POST_SNAPBACK}>

The problem is Python's scoping and declaration rules. That assignment statement is creating a new variable named query_info in the local scope. Python then interprets the query_info on the right-hand side as a reference to the local, as-yet-undefined, query_info. It works when the code in the global scope because the variable is already defined and so the assignment is just a simple assignment.

Yet another reason why I don't use Python.

Edited by jcl

Share this post


Link to post
Share on other sites

Thanks :-)

for some reason I was thinking any variable created in the global namespace was automatically able to be changed from within a function. After reading a little from my book, global variables can only be read from the functions namespace, not changed. They can be changed, but the varibale needs to be decrared global inside the functions namespace.

edit added later//

their was no good reason to change my global varible anyways. I did the code in a much cleaner way. Python is good for us beginner programmers. I get the impression it is desighned in a way to make people use good habits.

I have a hunch that is part of the reason you don't prefer it(python). It forces you to do stuff(like indenting) in a resticted manner, even thought it is not really nesseasary :blink:

Edited by shanenin

Share this post


Link to post
Share on other sites
Yet another reason why I don't use Python.

<{POST_SNAPBACK}>

I usually only here positives about python. Any thing else in particular that makes it undesireable?

Share this post


Link to post
Share on other sites
I usually only here positives about python. Any thing else in particular that makes it undesireable?

<{POST_SNAPBACK}>

It's a religious issue. Opinions about Python tend toward extremes: you either love it or hate it. There seem to be about equal numbers of people in both camps. There's nothing objectively wrong with it.

Edited by jcl

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this