通常可以使用try/except塊來運行一堆語句,直到其中一個語句導致異常。運行幾個命令,直到不會發生異常? (Python)
我想做相反的事 - 運行一組語句,其中每個語句都可能導致異常,但其中一個不會。
下面是一些僞代碼:
try:
c = 1/0 # this will cause an exception
c = 1/(1 - 1) # this will cause an exception
c = int("i am not an integer")
# this will cause an exception
c = 1 # this will not cause an exception
c = 2 # this statement should not be reached
c = None # this would be a final fallback in case everything exceptioned
print c # with this code, c should print "1"
我想用這樣的方式是數據分析。用戶可以提供一些數據,這些數據可以是幾種不同格式之一。如果數據與格式不匹配,試圖解析各種格式將產生異常。可能有幾十種不同的可能格式。這些陳述將按照優先順序列出。只要其中一個解析器成功,那就是我想要的變量結果。
包裝每嘗試裏面嘗試/ excepts會導致一些醜陋的意大利麪代碼。
c = None
try:
c = 1/0
except:
pass
if (c == None):
try:
c = 1/(1 - 1)
except:
pass
if (c == None):
try:
c = int("i am not an int")
except:
pass
... and so on
有沒有更好的方法來做到這一點?
我沒有看到你的意大利麪代碼在哪裏,最多隻有兩個縮進級別。 –
也許這是一個錯誤的術語,但無論哪種方式,所有這些如果/嘗試/除了塊更難以閱讀比如果你可以只有一個語句列表... – fdmillion