赋值前引用的局部变量? [英] Local variable referenced before assignment?
问题描述
我正在使用 PyQt 库截取网页的屏幕截图,然后读取不同 URL 的 CSV 文件.我保留了一个可变提要,每次处理 URL 时都会增加该提要,因此应该增加到 URL 的数量.
I am using the PyQt library to take a screenshot of a webpage, then reading through a CSV file of different URLs. I am keeping a variable feed that incremements everytime a URL is processed and therefore should increment to the number of URLs.
代码如下:
webpage = QWebPage()
fo = open("C:/Users/Romi/Desktop/result1.txt", "w")
feed = 0
def onLoadFinished(result):
#fo.write( column1[feed])#, column2[feed], urls[feed])
#feed = 0
if not result:
print "Request failed"
fo.write(column1[feed])
fo.write(',')
fo.write(column2[feed])
fo.write(',')
#fo.write(urls[feed])
fo.write(',')
fo.write('404,image not created\n')
feed = feed + 1
sys.exit(1)
save_page(webpage, outputs.pop(0)) # pop output name from list and save
if urls:
url = urls.pop(0) # pop next url to fetch from list
webpage.mainFrame().load(QUrl(url))
fo.write(column1[feed])#,column2[feed],urls[feed],'200','image created','/n')
fo.write(',')
fo.write(column2[feed])
fo.write(',')
#fo.write(urls[feed])
fo.write(',')
fo.write('200,image created\n')
feed = feed + 1
else:
app.quit() # exit after last url
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl(urls.pop(0)))
#fo.close()
sys.exit(app.exec_())
它给了我错误:
local variable feed referenced before the assignment at fo.write(column1[feed])#,column2[feed],urls[feed],'200','image created','/n')
知道为什么吗?
推荐答案
当 Python 在解析函数定义体时遇到诸如
When Python parses the body of a function definition and encounters an assignment such as
feed = ...
Python 默认将 feed
解释为局部变量.如果你不希望它是一个局部变量,你必须把
Python interprets feed
as a local variable by default. If you do not wish for it to be a local variable, you must put
global feed
在函数定义中.global 语句不必位于函数定义的开头,而是通常放置的位置.无论放在何处,全局声明都会使 feed
成为函数中无处不在的全局变量.
in the function definition. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes feed
a global variable everywhere in the function.
没有global语句,因为feed
被当成局部变量,Python执行时
Without the global statement, since feed
is taken to be a local variable, when Python executes
feed = feed + 1,
Python 首先评估右侧并尝试查找 feed 的值.第一次通过它发现 feed
是未定义的.因此出现错误.
Python evaluates the right-hand side first and tries to look up the value of feed. The first time through it finds feed
is undefined. Hence the error.
修补代码的最短方法是在onLoadFinished
的开头添加global feed
.更好的方法是使用类:
The shortest way to patch up the code is to add global feed
to the beginning of onLoadFinished
. The nicer way is to use a class:
class Page(object):
def __init__(self):
self.feed = 0
def onLoadFinished(self, result):
...
self.feed += 1
具有改变全局变量的函数的问题在于它使您的代码更难理解.函数不再是孤立的单元.它们的交互扩展到影响全局变量或受全局变量影响的所有事物.因此,它使更大的程序更难理解.
The problem with having functions which mutate global variables is that it makes it harder to grok your code. Functions are no longer isolated units. Their interaction extends to everything that affects or is affected by the global variable. Thus it makes larger programs harder to understand.
通过避免改变全局变量,从长远来看,您的代码将更易于理解、测试和维护.
By avoiding mutating globals, in the long run your code will be easier to understand, test and maintain.
这篇关于赋值前引用的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!