Skip to content

while-one (UP048)

Preview (since 0.16.3) · Related issues · View source

Derived from the pyupgrade linter.

Fix is always available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for while loops that use 1 as their condition.

Why is this bad?

while 1: is a Python 2 idiom, where True was a global that could be rebound and so had to be loaded and tested on every iteration. In Python 3 True is a keyword, so both spellings compile to the same bytecode and while True: is clearer about the loop being infinite.

Example

while 1:
    print("Hello, world!")

Use instead:

while True:
    print("Hello, world!")

References