About local variable ' has ' referenced before assignment problem

Source: Internet
Author: User
Today, when Django is developed, the access page always has an error message.
Local variable ' has ' referenced before assignment
"Look at the data, it seems to say that you can't access the variable, check the code my view is written like this:

def musictable (Request):
musicians = [
{' name ': ' Django Reinhardt ', ' genre ': ' Jazz '},
{' name ': ' Jimi Hendrix ', ' genre ': ' Rock '},
{' name ': ' Louis Armstrong ', ' genre ': ' Jazz '},
{' name ': ' Pete Townsend ', ' genre ': ' Rock '},
{' name ': ' Yanni ', ' genre ': ' New Age '},
{' name ': ' Ella Fitzgerald ', ' genre ': ' Jazz '},
{' name ': ' Wesley Willis ', ' genre ': ' Casio '},
{' name ': ' John Lennon ', ' genre ': ' Rock '},
{' name ': ' Bono ', ' genre ': ' Rock '},
{' name ': ' Garth Brooks ', ' genre ': ' Country '},
{' name ': ' Duke Ellington ', ' genre ': ' Jazz '},
{' name ': ' William Shatner ', ' genre ': ' Spoken word '},
{' name ': ' Madonna ', ' genre ': ' Pop '},]
Mu=[]
#预处理 to determine whether bold display, templates are only rendered, should not be processed to determine what is special display
For M in musicians:
If ' not in m[' name ':
has = True
Mu.append ({' name ': m[' name '),
' Genre ': m[' genre '],
' Is_important ': m[' genre '] in (' Jazz ', ' Rock '),
' is_pretentious ': ' Not in m[' name ',}
)
Return Render_to_response (' musictable.html ', {' mu ': mu, ' has_pretentious ': Has,})

A sharp look at the variable has should be a value ah, I am depressed.
Then I saw a post on the Internet that was the problem.
-------------------------------------------------------------------------------

The program is roughly like this:

CONSTANT = 0

Def modifyconstant ():
Print CONSTANT
CONSTANT + 1
Return

if __name__ = = ' __main__ ':
Modifyconstant ()
Print CONSTANT

The results of the operation are as follows:
Unboundlocalerror:local variable ' CONSTANT ' referenced before Assignment

It seems that the global variable in the function modifyconstant as a local variable, it seems that the global variable does not take effect.
Make some changes:

CONSTANT = 0

Def modifyconstant ():
Print CONSTANT
#constant + 1
Return

if __name__ = = ' __main__ ':
Modifyconstant ()
Print CONSTANT

Functioning normally, it seems that the function is internally accessible to global variables.
So the problem is, because the variable constant is modified inside the function, PythonConstant is considered a local variable, and print constant is before constant + + 1, so of course this error occurs.

So, how do you access and modify global variables inside a function? You should use the keyword global to modify variables (a bit like PHP):

CONSTANT = 0

Def modifyconstant ():
Global CONSTANT
Print CONSTANT
CONSTANT + 1
Return

if __name__ = = ' __main__ ':
Modifyconstant ()
Print CONSTANT

It's as simple as that.

------------------------------------------------------------------------------------
Read the top post content, I have a little inspiration, take a closer look at my program here:
For M in musicians:
If ' not in m[' name ':
has = True
Mu.append ({' name ': m[' name '),
' Genre ': m[' genre '],
' Is_important ': m[' genre '] in (' Jazz ', ' Rock '),
' is_pretentious ': ' Not in m[' name ',}
)
Return Render_to_response (' musictable.html ', {' mu ': mu, ' has_pretentious ': Has,})

There is no space in the red part, and there is no one at all in this loop that satisfies the if ' not in m[' name ': This condition, so that the return render_to_response (' musictable.html ', {' mu ': mu, ' Has_pretentious ': Has,}) when passing the has, the error.
The solution has two is to convert if ' not in m[' name ': ' Plus space ' into '.
The second option is to give has an initial value of Has=false.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.