Why were True and False changed to keywords in Python 3

前端 未结 3 661
忘了有多久
忘了有多久 2020-12-01 10:07

In Python 2, we could reassign True and False (but not None), but all three (True, False, and None

3条回答
  •  既然无缘
    2020-12-01 10:50

    For two reasons, mainly:

    1. So people don't do a __builtin__.True = False prank hidden on a random module. (as explayned by devnull)
    2. Because keywords are faster than global builtins. In Python 2.x, the interpreter would have to resolve those variables' values before using them, which is a bit slower than keywords. (see Why is if True slower than if 1?)

提交回复
热议问题