PyCharm 4.0.0 Web Help

The Extract Field refactoring declares a new field and initializes it with the selected expression. The original expression is replaced with the usage of the field.

Example

Before After
import math

class Solver:
    def roots(self):
        a = 3
        b = 25
        c = 46
        root1 = (-b + math.sqrt(b**2 - 4*c)) / (2*a)
        root2 = (-b - math.sqrt(b**2 - 4*c)) / (2*a)
        print (root1, root2)

Solver().demo()
import math

class Solver:
    def demo(self):
        a = 3
        b = 25
        c = 46
        self.return_type_of_sqrt = math.sqrt(b ** 2 - 4 * a * c)
        root1 = (-b + self.return_type_of_sqrt) / (2*a)
        root2 = (-b - self.return_type_of_sqrt) / (2*a)
        print(root1,root2)

Solver().demo()
py_extract_substring1 py_extract_substring_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. Place the cursor within the expression or declaration of a variable to be replaced by a field.
  2. Do one of the following:
    • Press Ctrl+Alt+FCtrl+Alt+FCtrl+Alt+FCtrl+Alt+FCtrl+Alt+FCtrl+Alt+FShift+Alt+ECtrl+Alt+FAlt ⌘ FAlt ⌘ FAlt ⌘ F.
    • Choose Refactor | Introduce Field in the main menu, or on the context menu.
  3. If more than one expression is detected for the current cursor position, the Expressions list appears. If this is the case, select the required expression. To do that, click the expression. Alternatively, use the Up and Down arrow keys to navigate to the expression of interest, and then press EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter to select it.

    IntroduceConstant_Python_InPlace_SelectExpression

  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:
    IntroduceConstant_Python_InPlace_ReplaceOccurrences
  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.

    IntroduceField_Python_InPlace_Init_Name

  7. To complete the refactoring, press TabTabTabTabTabTabTabTabTabTabTab or EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter.

    If you haven't completed the refactoring and want to cancel the changes you have made, press EscapeEscapeEscapeEscapeEscape, Escape or Ctrl+GEscapeEscapeEscapeEscapeEscapeEscape.

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

    IntroduceField_Python_InPlace_Result
To extract a field using the dialog box

If the Enable in place refactorings check box is cleared on the Editor settings, the Extract Field refactoring is performed by means of the Introduce Field dialog box.

enableInplaceRefactoringCleared

  1. In the editor, select the expression or variable to be replaced with a field, or just place the cursor 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+FCtrl+Alt+FCtrl+Alt+FCtrl+Alt+FCtrl+Alt+FCtrl+Alt+FShift+Alt+ECtrl+Alt+FAlt ⌘ FAlt ⌘ FAlt ⌘ F.
  3. In the Expressions pop-up menu, select the expression to be replaced. Note that PyCharm highlights the selected expression in the editor.
  4. In the Extract Field Dialog 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.

See Also

Reference:

Web Resources: