PyCharm 2025.2 Help

Introduce Field

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

Introduce an attribute

  1. Place the caret within a piece of code you want to introduce as an attribute.

  2. Press Ctrl+Alt+F or go to Refactor | Extract/Introduce | Field in the main menu. Alternatively, right-click and select Refactor | Field 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, use the Up and Down arrow keys to navigate to the expression of interest and then press Enter to select it.

    Select an expression

    If PyCharm detects more than one occurrence in your code, it lets you specify which occurrences to replace.

    Extract multiple occurrences of a field
  4. If relevant, specify where the new attribute will be initialized—in the current method, or in a class constructor.

  5. Specify the name of the attribute. Select the name from the list or type the name in the box.

    Select the name

    To complete the refactoring, press Tab or Enter.

Example

import math class SolverEquation: def demo(self): a = 3 b = 25 c = 46 root1 = (-b + math.sqrt(b**2 - 4*a*c)) / (2*a) root2 = (-b - math.sqrt(b**2 - 4*a*c)) / (2*a) print(root1, root2)
import math class SolverEquation: def demo(self): a = 3 b = 25 c = 46 self.math_sqrt = math.sqrt(b ** 2 - 4 * a * c) root1 = (-b + self.math_sqrt) / (2 * a) root2 = (-b - self.math_sqrt) / (2 * a) print(root1, root2)
24 July 2025