Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

python - Another UnboundLocalError in Python2.7

When I execute a testing script in my company's Python project, I got an error as below:

UnboundLocalError: local variable 'a' referenced before assignment

I wrote some simpler code to reproduce the issue, it has 2 files.

vars.py file:

#!/usr/bin/env python
a = 'aaa'

script.py file:

#!/usr/bin/env python
from vars import *


def myFunc1():
    print a

    if False:
        a = '111'

    print a

myFunc1()

Execute the code:

$ python --version
Python 2.7.10
$ python script.py 
Traceback (most recent call last):
  File "script.py", line 13, in <module>
    myFunc1()
  File "script.py", line 6, in myFunc1
    print a
UnboundLocalError: local variable 'a' referenced before assignment
$ 

I googled the UnboundLocalError and found some useful information like:

UnboundLocalError: local variable 'L' referenced before assignment Python

UnboundLocalError in Python

According to the answers in above 2 questions, if I add a global a after the def myFunc1(): line in script.py file, the error is gone.

The thing I don't understand is removing the if condition from myFunc1 can also make it work...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assigning to a name makes the name a local variable, which means that you can't assign to a non-local variable without extra syntax. In your code:

from vars import *

def myFunc1():
    print a         # the local variable `a` is used before it is created
    if False:
        a = '111'   # this creates a local variable `a`
    print a

adding global a as the first line in myFunc1 will tell Python that it shouldn't create a local variable when it sees an assignment to a. It will almost certainly not do what you expect though (assuming you expect the a in vars to be changed..). from vars import * creates local "copies" of the names in vars, and using the global statement just means that you are assigning to this module's a variable. Other modules that import vars will not see the assignment.

Removing the if statements also removes the assignment, which is why that eliminates the error.

I understand that from vars import * and using variables in the vars.py is not a good design... But I can't pass all needed variables to the function since the function may use 20+ variables from the vars.py in company's project

shudder.. please refactor.

For this particular problem you should use this pattern:

import vars

def myFunc1():
    print vars.a
    if False:
        vars.a = '111'
    print vars.a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...