PyCharm 2025.2 Help

Invert boolean

This refactoring aims to flip the value of a Boolean variable and all its usages from True to False and vice versa.

Example

Before

After

def foo(): abc = True return abc
def foo(): abc = False return not abc

To invert a Boolean variable, follow these steps

  1. Place the caret at the name of a Boolean variable.

  2. Go to Refactor | Invert Boolean.

  3. In the dialog that opens, click Preview to view the suggested changes in the Find tool window, or Refactor to invert the detected occurrences silently.

11 February 2024