PyCharm 2024.1 Help

Extract/Introduce variable

If you come across an expression that is hard to understand or it is duplicated in several places throughout your code, the Extract Variable refactoring Ctrl+Alt+V can help you deal with those problems placing the result of such expression or its part into a separate variable that is less complex and easier to understand. Plus, it reduces the code duplication.

You can use the Introduce Variable refactoring to extract variadic arguments into a new slice variable.

Extract a variable in place

  1. In the editor, select the expression to be replaced with a variable. You can do that yourself or use the smart expression selection feature to let PyCharm help you. So, do one of the following:

    • Highlight the expression. Then choose Refactor | Extract | Variable from the main menu or from the context menu.

      Alternatively, press Ctrl+Alt+V.

    • Place the caret before or within the expression. Choose Refactor | Introduce Variable from the main menu or from the context menu, or press Ctrl+Alt+V.

      IntroduceConstant_Python_InPlace_SelectExpression.png
  2. If more than one occurrence of the selected expression is found, select Replace this occurrence only or Replace all occurrences in the Multiple occurrences found popup menu. To select the required option, just click it. Alternatively, use the Up and Down arrow keys to navigate to the option of interest, and press Enter to select it.

    IntroduceConstant_Python_InPlace_ReplaceOccurrences.png
  3. Specify the name of the variable. Do one of the following:

    • Select one of the suggested names from the list. To do that, double-click the suitable name. Alternatively, use the Up and Down arrow keys to navigate to the name of interest, and Enter to select it.

      introduceVariablePythonSelectName.png
    • Edit the name by typing. The name is shown in the box with red borders and changes as you type. When finished, press Enter.

      introduceVariablePythonTypeName.png

Extract a variable using the dialog

If the In modal dialogs refactoring option is selected in the Refactorings area on the Code Editing page of the Settings dialog (Ctrl+Alt+S) , the Introduce Variable refactoring is performed by means of the Extract Variable Dialog.

the Enable in-place mode setting
  1. In the editor, select the expression to be replaced with a variable. You can do that yourself or use the smart expression selection feature to let PyCharm help you. So, do one of the following:

    • Highlight the expression. Then choose Refactor | Extract | Variable from the main menu or from the context menu.

      Alternatively, press Ctrl+Alt+V.

    • Place the caret before or within the expression. Choose Refactor | Introduce Variable from the main menu or from the context menu, or press Ctrl+Alt+V.

      From the Expressions list, select the expression. To do that, click the required expression. Alternatively, use the Up and Down arrow keys to navigate to the expression of interest, and then press Enter to select it.

  2. In the Extract Variable Dialog dialog:

    1. Specify the variable name next to Name field. You can select one of the suggested names from the list or type the name in the Name box.

    2. If more than one occurrence of the selected expression is found, you can select to replace all the found occurrences by selecting the corresponding checkbox. If you want to replace only the current occurrence, clear the Replace all occurrences checkbox.

    3. Click OK.

Last modified: 27 June 2024