When the Extract Method refactoring is invoked , PyCharm analyses the selected block of code and detects variables that are the input for the selected code fragment and the variables that are output for it.
The detected output variable is used as a return value for the extracted function.
To extract a method
- In the editor, select a block of code to be transformed into a method or a function.
- On the main menu or on the context menu of the selection, choose Refactor | Extract | Method or press Ctrl+Alt+M.
- In the Extract Method dialog box that opens, specify the name of the new method.
Parameters area, do the following:
- Specify the variables to be passed as method parameters by selecting/clearing the corresponding check boxes.
- Rename the desired parameters, by double-clicking the corresponding parameter lines and entering new names.
- Check the result in the
Signature Preview pane and click
to create the method.
The selected code fragment will be replaced with a method call.