How to create an 'empty if statement' in python

后端 未结 4 954
别那么骄傲
别那么骄傲 2020-12-05 17:45

It\'s very common in C: hacking \'empty if statement\' like this:

if(mostlyhappencondition)
    ;#empty statement
else{
    dosomething;
}

4条回答
  •  日久生厌
    2020-12-05 17:56

    I can only guess you're looking for the pass statement, sometimes needed to create an empty code block to prevent a syntax error.

    if mostlyhappencondition:
        pass
    else:
        do_something()
    

    It would be much more usual to just do this, which is logically equivalent:

    if not mostlyhappencondition:
        do_something()
    

    There are no significant performance gains to be found here.

提交回复
热议问题