How to exit an if clause

前端 未结 13 1769
故里飘歌
故里飘歌 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

    Effectively what you're describing are goto statements, which are generally panned pretty heavily. Your second example is far easier to understand.

    However, cleaner still would be:

    if some_condition:
       ...
       if condition_a:
           your_function1()
       else:
           your_function2()
    
    ...
    
    def your_function2():
       if condition_b:
           # do something
           # and then exit the outer if block
       else:
           # more code here
    

提交回复
热议问题