PyCharm 2021.1 Help

Extract field

The Extract Field refactoring lets you declare a new field and initialize it with the selected expression. The original expression is replaced with the usage of the field.

Extract a field using the dialog

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

Enable inplace refactoring cleared
  1. In the editor, select the expression or variable to be replaced with a field, or just position the caret within such an expression or variable declaration.

  2. In the main menu, or the context menu of the selection, choose Refactor | Extract | Field, or press Ctrl+Alt+F.

  3. From the Expressions list, select the expression to be replaced. Note that PyCharm highlights the selected expression in the editor.

  4. In the dialog that opens, specify the type and name of the new field.

  5. In the Initialize in section, specify where the new field will be initialized.

  6. To automatically replace all occurrences of the selected expression (if it is found more than once), select the option Replace all occurrences.

  7. Click OK to create the field.

To extract a field in-place

The in-place refactorings are enabled in PyCharm by default. So, if you haven't changed this setting, the Introduce Field refactorings are performed in-place, right in the editor:

  1. Position the caret within the expression or declaration of a variable to be replaced by a field.

  2. Do one of the following:

    • Press Ctrl+Alt+F.

    • Choose Refactor | Introduce Field from the main menu, or from the context menu.

  3. If more than one expression is detected for the current caret position, the Expressions list appears. If this is the case, click the expression to select it. Alternatively, press Up or Down to navigate to the expression of interest, and then press Enter to select it.

    Introduce constant python in place select expression

  4. If more than one occurrence of the expression is found within the class, specify whether you wish to replace only the selected occurrence, or all the found occurrences with the new constant:

    Introduce constant python in place replace occurrences

  5. If relevant, specify where the new field will be initialized - in the current method, or in a class constructor.

  6. Specify the name of the field. Select the name from the list or type the name in the box with a red border.

    Introduce field python in place init name

  7. To complete the refactoring, press Tab or Enter.

    If you haven't completed the refactoring and want to cancel the changes you have made, press Escape.

    Note that sometimes you may need to press the corresponding key more than once.

    Introduce field python in place result

Example

class Hello def greet puts "Hello from JetBrains" end def bye puts "Goodbye from JetBrains" end end
class Hello def initialize @name = "JetBrains" end def greet puts "Hello from #{@name}" end def bye puts "Goodbye from #{@name}" end end

Last modified: 08 March 2021