如何在python中创建一个“空if语句”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15081516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:16:29  来源:igfitidea点击:

How to create an 'empty if statement' in python

pythonif-statement

提问by JJP

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

这在 C 中很常见:像这样破解“空 if 语句”:

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

It's working in Python? I mean, can we improve the app's performance by using that? I'd also like to know why.

它在 Python 中工作吗?我的意思是,我们可以通过使用它来提高应用程序的性能吗?我也想知道为什么。

采纳答案by koblas

There is a performance improvement if there isn't an else case in the "if", since the bytecodes don't pass execution into the "if" case.

如果“if”中没有 else 情况,则性能会有所提高,因为字节码不会将执行传递到“if”情况中。

Here's some functions and the output of dis.dis(foo)

这是一些函数和输出 dis.dis(foo)

The following sample app:

以下示例应用程序:

def foo(x):
    if x:
        pass
    else:
        return x+2

Disassembles to:

拆卸为:

5           0 LOAD_FAST                0 (x)
            3 POP_JUMP_IF_FALSE        9

6           6 JUMP_FORWARD             8 (to 17)

8     >>    9 LOAD_FAST                0 (x)
           12 LOAD_CONST               1 (2)
           15 BINARY_ADD          
           16 RETURN_VALUE        
      >>   17 LOAD_CONST               0 (None)
           20 RETURN_VALUE        

The following

下列

def foo(x):
    if not x:
        return x+2

Disassembles to:

拆卸为:

11           0 LOAD_FAST                0 (x)
             3 POP_JUMP_IF_TRUE        14

12           6 LOAD_FAST                0 (x)
             9 LOAD_CONST               1 (2)
            12 BINARY_ADD          
            13 RETURN_VALUE        
       >>   14 LOAD_CONST               0 (None)

回答by Ry-

No, that won't improve performance. In fact, it doesn't in C, either. Where did you hear that?

不,这不会提高性能。事实上,它也不在 C 中。你是从哪里听来的?

not/!reads better and should have more or less the same speed.

not/!读起来更好,速度应该差不多。



And actually tested with gcc -O4:

并实际测试gcc -O4

#include <stdio.h>

int main(int argc, char *argv[]) {
    for(int i = 0; i < 1000000000; i++) {
        if(!(i < 900000000)) {
            putchar('.');
        }
    }
}

vs.

对比

#include <stdio.h>

int main(int argc, char *argv[]) {
    for(int i = 0; i < 1000000000; i++) {
        if(i < 900000000);
        else {
            putchar('.');
        }
    }
}

#1 took 6.62 seconds and #2 took 6.64 seconds on my computer.

在我的电脑上,#1 用了 6.62 秒,#2 用了 6.64 秒。

回答by wim

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

我只能猜测您正在寻找该pass语句,有时需要创建一个空代码块以防止语法错误。

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.

这里没有显着的性能提升。

回答by Kaunteya

Till the time you dont have any thing in if True:you could do

直到你没有任何东西if True:可以做的时候

if not mostlyhappenedcondition:
   do something()

If you dont have to put any thing in "if" even in future, then it is redundunt in your code.

如果您将来不必在“if”中添加任何内容,那么它在您的代码中就是多余的。