簡體   English   中英

如何在 Python 中的 if/else 語句中退出 try/except

[英]How to exit from a try/except inside of an if/else statement in Python

所以我有這個:

if condition:
    ...
elif condition2:
    ...
elif condition3:
    try:
        existing_bid = # get some object from django models 
    except SomeError: # if this object does not exist
        # here I need to escape from if/else to main scope
    if ... # some comparison with an existing_bid:
        #return error
    ... # try to manipulate existing_bid object and save it
# here I need to escape
...
...

什么是從嘗試/除了在主 scope 的 if/else 內部逃脫的最佳實踐? 謝謝

編輯:
提供了一些上下文

如果你想在 try/except 之后逃跑並回到主 scope。 如果將 try/except 語句放在elif條件 3 的底部,將很容易處理。一旦完成try/except語句,程序也會完成elif語句並轉義到主 scope。

否則,你必須在你的try/except代碼中break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM