How to exit an if clause

前端 未结 13 1748
故里飘歌
故里飘歌 2020-12-04 08:45

What sorts of methods exist for prematurely exiting an if clause?

There are times when I\'m writing code and want to put a break statement

13条回答
  •  独厮守ぢ
    2020-12-04 08:47

    You can emulate goto's functionality with exceptions:

    try:
        # blah, blah ...
        # raise MyFunkyException as soon as you want out
    except MyFunkyException:
        pass
    

    Disclaimer: I only mean to bring to your attention the possibility of doing things this way, while in no way do I endorse it as reasonable under normal circumstances. As I mentioned in a comment on the question, structuring code so as to avoid Byzantine conditionals in the first place is preferable by far. :-)

提交回复
热议问题