尝试...除了...最终Python 2.5中的问题 [英] try...except...finally problem in Python 2.5

查看:53
本文介绍了尝试...除了...最终Python 2.5中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继续收到此错误本地变量''f''在

赋值之前引用'当我运行以下代码时,在finally块中。


尝试:

f = file(self.filename,''rb'')

f.seek(DATA_OFFSET)

self .__ data = f.read(DATA_SIZE)

self.isDataLoaded = True

除外:

self.isDataLoaded = False

终于:

f.close()


可以有人告诉我代码有什么问题?我做的事情

错了吗?我对python有些新意,但这对我来说很有意义。

I keep getting this error "local variable ''f'' referenced before
assignment" in the finally block when I run the following code.

try:
f = file(self.filename, ''rb'')
f.seek(DATA_OFFSET)
self.__data = f.read(DATA_SIZE)
self.isDataLoaded = True
except:
self.isDataLoaded = False
finally:
f.close()

Can someone tell me what''s wrong with the code? Am I doing something
wrong? I''m somewhat new to python but this makes sense to me.

推荐答案

" redawgts" < re ****** @ gmail.com写信息

新闻:11 ********************* @ l53g2000cwa。 googlegro ups.com ...
"redawgts" <re******@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...

>我继续收到此错误本地变量''f''之前引用

赋值" ;当我运行以下代码时,在finally块中。


尝试:

f = file(self.filename,''rb'')

f.seek(DATA_OFFSET)

self .__ data = f.read(DATA_SIZE)

self.isDataLoaded = True

除外:

self.isDataLoaded = False

终于:

f.close()


可以有人告诉我代码有什么问题?我做的事情

错了吗?我对python有些新鲜,但这对我来说很有意义。
>I keep getting this error "local variable ''f'' referenced before
assignment" in the finally block when I run the following code.

try:
f = file(self.filename, ''rb'')
f.seek(DATA_OFFSET)
self.__data = f.read(DATA_SIZE)
self.isDataLoaded = True
except:
self.isDataLoaded = False
finally:
f.close()

Can someone tell me what''s wrong with the code? Am I doing something
wrong? I''m somewhat new to python but this makes sense to me.



如果对文件的调用引发异常,那么变量f就不会被创建。


这里有一个简单的例子:


def f():

抛出42

尝试:

x = f()

除外:

打印x


尝试一下,看看会发生什么。当你在它的时候,你可能会想要打印出你想要打印的东西。

If the call to file raises an exception, then variable f won''t have been
created.

Here''s a simple example:

def f():
throw 42
try:
x = f()
except:
print x

Try it and see what happens. While you''re at it, you might try to figure
out what you would like it to print.


redawgts写道:
redawgts wrote:

我一直收到这个错误"局部变量'f''之前引用

赋值"当我运行以下代码时,在finally块中。


尝试:

f = file(self.filename,''rb'')

f.seek(DATA_OFFSET)

self .__ data = f.read(DATA_SIZE)

self.isDataLoaded = True

除外:

self.isDataLoaded = False

终于:

f.close()


可以有人告诉我代码有什么问题?我做的事情

错了吗?我对python有些新鲜,但这对我来说很有意义。
I keep getting this error "local variable ''f'' referenced before
assignment" in the finally block when I run the following code.

try:
f = file(self.filename, ''rb'')
f.seek(DATA_OFFSET)
self.__data = f.read(DATA_SIZE)
self.isDataLoaded = True
except:
self.isDataLoaded = False
finally:
f.close()

Can someone tell me what''s wrong with the code? Am I doing something
wrong? I''m somewhat new to python but this makes sense to me.



移动f = file(self.filename,''rb'')"上面的尝试:如果打开文件

恰好抛出异常,那么分配将永远不会发生,并且

将不会''f''关闭。
< br $> b $ b -

Robert Kern


我开始相信整个世界都是一个谜,一个无害的谜团<由于我们疯狂地试图将它解释为一个潜在的真相,因为它是可怕的。

- Umberto Eco

Move the "f = file(self.filename, ''rb'')" above the try:. If opening the file
happens to throw an exception, then the assignment will never happen and there
will be no ''f'' to close.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


redawgts写道:
redawgts wrote:

我一直收到此错误本地变量''f' '之前引用

赋值'当我运行以下代码时,在finally块中。


尝试:

f = file(self.filename,''rb'')

f.seek(DATA_OFFSET)

self .__ data = f.read(DATA_SIZE)

self.isDataLoaded = True

除外:

self.isDataLoaded = False

终于:

f.close()


可以有人告诉我代码有什么问题?我做的事情

错了吗?我对python有些新意,但这对我来说很有意义。
I keep getting this error "local variable ''f'' referenced before
assignment" in the finally block when I run the following code.

try:
f = file(self.filename, ''rb'')
f.seek(DATA_OFFSET)
self.__data = f.read(DATA_SIZE)
self.isDataLoaded = True
except:
self.isDataLoaded = False
finally:
f.close()

Can someone tell me what''s wrong with the code? Am I doing something
wrong? I''m somewhat new to python but this makes sense to me.



finally:即使在这种情况下有异常,也会执行block

尚未定义。你想要的是:


试试:

f = file(self.filename,''rb'')

f。寻求(DATA_OFFSET)

self .__ data = f.read(DATA_SIZE)

self.isDataLoaded = True


除了:

isDataLoaded = False


else:

f.close()


- 拉里

finally: block is executed even if there is an exception in which case f
hasn''t been defined. What you want is:

try:
f = file(self.filename, ''rb'')
f.seek(DATA_OFFSET)
self.__data = f.read(DATA_SIZE)
self.isDataLoaded = True

except:
isDataLoaded=False

else:
f.close()

-Larry


这篇关于尝试...除了...最终Python 2.5中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆