“while 1"和“while 1"有什么区别?和“虽然是真的"? [英] What's the difference between "while 1" and "while True"?

查看:25
本文介绍了“while 1"和“while 1"有什么区别?和“虽然是真的"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过两种在 Python 中创建无限循环的方法:

  1. while 1:做一点事()

  2. while True:做一点事()

这两者有什么区别吗?一个比另一个更pythonic吗?

解决方案

从根本上说没有关系,这些细节并不真正影响某些东西是否是pythonic".

但是,如果您对琐事感兴趣,这里有一些不同之处.

  1. 内置布尔类型直到Python 2.3 所以打算在旧版本上运行的代码倾向于使用 while 1: 形式.例如,您将在标准库中看到它.

  2. True 和 False 内置函数是 不是保留字在 Python 3 之前,因此可以分配给,更改它们的值.这有助于解决上述情况,因为代码可以执行 True = 1 以实现向后兼容性,但这意味着每次需要在全局字典中查找名称 True使用.

  3. 由于上述限制,两个版本编译成的字节码在 Python 2 中是不同的,因为它对不能用于 True 的常量整数进行了优化.因为 Python 在编译 1 时可以判断它总是非零,所以它删除了条件跳转并且根本不加载常量:

    <预><代码>>>>导入文件>>>定义 while_1():...而1:... 经过...>>>def while_true():...而真:... 经过...>>>dis.dis(while_1)2 0 SETUP_LOOP 5(到 8)3 >>3 JUMP_ABSOLUTE 36 POP_TOP7 POP_BLOCK>>8 LOAD_CONST 0(无)11 RETURN_VALUE>>>dis.dis(while_true)2 0 SETUP_LOOP 12(到 15)>>3 LOAD_GLOBAL 0(真)6 JUMP_IF_FALSE 4(到 13)9 POP_TOP3 10 JUMP_ABSOLUTE 3>>13 POP_TOP14 POP_BLOCK>>15 LOAD_CONST 0 (无)18 RETURN_VALUE

所以,while True: 更容易阅读,而 while 1: 对旧版本的 Python 更友好一些.由于您现在不太可能需要在 Python 2.2 上运行或需要担心循环的字节码计数,因此前者稍微好一些.

I've seen two ways to create an infinite loop in Python:

  1. while 1:
        do_something()
    

  2. while True:
        do_something()
    

Is there any difference between these? Is one more pythonic than the other?

解决方案

Fundamentally it doesn't matter, such minutiae doesn't really affect whether something is 'pythonic' or not.

If you're interested in trivia however, there are some differences.

  1. The builtin boolean type didn't exist till Python 2.3 so code that was intended to run on ancient versions tends to use the while 1: form. You'll see it in the standard library, for instance.

  2. The True and False builtins are not reserved words prior to Python 3 so could be assigned to, changing their value. This helps with the case above because code could do True = 1 for backwards compatibility, but means that the name True needs to be looked up in the globals dictionary every time it is used.

  3. Because of the above restriction, the bytecode the two versions compile to is different in Python 2 as there's an optimisation for constant integers that it can't use for True. Because Python can tell when compiling the 1 that it's always non-zero, it removes the conditional jump and doesn't load the constant at all:

    >>> import dis
    >>> def while_1():
    ...     while 1:
    ...         pass
    ...
    >>> def while_true():
    ...     while True:
    ...         pass
    ...
    >>> dis.dis(while_1)
      2           0 SETUP_LOOP               5 (to 8)
    
      3     >>    3 JUMP_ABSOLUTE            3
                  6 POP_TOP
                  7 POP_BLOCK
            >>    8 LOAD_CONST               0 (None)
                 11 RETURN_VALUE
    >>> dis.dis(while_true)
      2           0 SETUP_LOOP              12 (to 15)
            >>    3 LOAD_GLOBAL              0 (True)
                  6 JUMP_IF_FALSE            4 (to 13)
                  9 POP_TOP
    
      3          10 JUMP_ABSOLUTE            3
            >>   13 POP_TOP
                 14 POP_BLOCK
            >>   15 LOAD_CONST               0 (None)
                 18 RETURN_VALUE
    

So, while True: is a little easier to read, and while 1: is a bit kinder to old versions of Python. As you're unlikely to need to run on Python 2.2 these days or need to worry about the bytecode count of your loops, the former is marginally preferable.

这篇关于“while 1"和“while 1"有什么区别?和“虽然是真的"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆