将嵌套的try / except语句转换为try / except / else [英] converting a nested try/except statement into try/except/else

查看:115
本文介绍了将嵌套的try / except语句转换为try / except / else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始这样:


试试:

if int(text)0:

return真的

否则:

self.error_message()

返回False

除了ValueError:

self.error_message()

返回False


我把它重写为:


尝试:

int(text)

除了ValueError:

self.error_message()

返回False

else:

返回True


我觉得它更干净,但显然我在原来的测试中输了

如果声明。


所以我的问题是,我是否仍然可以保留第二个结构并仍然可以测试0,但没有任何额外的嵌套?


谢谢。

I''m starting out with this:

try:
if int(text) 0:
return True
else:
self.error_message()
return False
except ValueError:
self.error_message()
return False

I rewrote it as this:

try:
int(text)
except ValueError:
self.error_message()
return False
else:
return True

I think it''s much cleaner, but obviously I lost the test in the original
if statement.

So my question is, can I still retain this second structure and still
test for 0, but not have any extra nesting?

Thanks.

推荐答案

John Salernoaécrit:
John Salerno a écrit :

我开始用这个:


试试:

if int(text)0:

返回True

else:

self.error_message()

返回False

除了ValueError:

self.error_message()

返回False


我把它重写为:


试试:

int(text)

除了ValueError:

self.error_message()

返回False

else:

返回True


我认为它更干净,但显然我在原来的测试中丢失了测试>
如果声明。


所以我的问题是,我是否仍然可以保留第二个结构并且仍然是
测试0,但没有任何额外的筑巢?
I''m starting out with this:

try:
if int(text) 0:
return True
else:
self.error_message()
return False
except ValueError:
self.error_message()
return False

I rewrote it as this:

try:
int(text)
except ValueError:
self.error_message()
return False
else:
return True

I think it''s much cleaner, but obviously I lost the test in the original
if statement.

So my question is, can I still retain this second structure and still
test for 0, but not have any extra nesting?



解决方案1:


def错误():

提高ValueError


尝试:

int(text)0或者错误()

除了ValueError:

self.error_message( )

返回False

否则:

返回True


但这是 - 太聪明的imho ...


解决方案2:

def error_message():

self.error_message ()

返回False


尝试:

返回int(文本)0或error_message()

除了ValueError:

return error_message()

solution 1:

def wrong():
raise ValueError

try:
int(text) 0 or wrong()
except ValueError:
self.error_message()
return False
else:
return True

But that''s being-too-clever imho...

solution 2:

def error_message():
self.error_message()
return False

try:
return int(text) 0 or error_message()
except ValueError:
return error_message()


John Salerno写道:
John Salerno wrote:

我开始用这个:


试试:

if int(text)0:

返回True

否则:

self.error_message()

返回False

除了ValueError:

self.error_message()

返回False


我把它重写为:


尝试:

int(文字)

除了ValueError:

self.error_message()

返回False

else:

返回True


我认为它更干净,但显然我在原来的

if语句中输了测试。


所以我的问题是,我是否仍然可以保留第二个结构并且仍然可以测试0,但是没有额外的嵌套?


谢谢。
I''m starting out with this:

try:
if int(text) 0:
return True
else:
self.error_message()
return False
except ValueError:
self.error_message()
return False

I rewrote it as this:

try:
int(text)
except ValueError:
self.error_message()
return False
else:
return True

I think it''s much cleaner, but obviously I lost the test in the original
if statement.

So my question is, can I still retain this second structure and still
test for 0, but not have any extra nesting?

Thanks.



我8天前给你的版本怎么样? ;-)

http ://groups.google.ca/group/comp.l...0fcd8932b0733a



和平,

~西蒙

What about the version I gave you 8 days ago? ;-)

http://groups.google.ca/group/comp.l...0fcd8932b0733a

It''s clean, does the job, and doesn''t have any extra nesting.

Peace,
~Simon


Bruno Desthuilliers a écrit:
Bruno Desthuilliers a écrit :

John Salernoaécrit:
John Salerno a écrit :



(snip)


或者当然是死的简单:


尝试:

if int(text)< = 0:raise ValueError

除了ValueError:

self.error_message()

返回False

else:

返回True

BTW,你真的应该看一下FormEncode ......

(snip)

or of course the dead simple:

try:
if int(text) <= 0: raise ValueError
except ValueError:
self.error_message()
return False
else:
return True
BTW, you really should have a look at FormEncode...


这篇关于将嵌套的try / except语句转换为try / except / else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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