Refactoring is a process of improving your source code without creating a new functionality. Refactoring helps you keep your code solid and easy to maintain.
To perform refactoring, follow these general steps
Select (or hover over) a symbol or code fragment to refactor. You can select symbols in the following PyCharm components:
Structure tool window
UML Class diagram
Do one of the following:
On the mainmenu or from the context menu of the selection, choose the desired refactoring or press the corresponding keyboard shortcut (if any).
Go to, or press Control+Alt+Shift+T, and then select the desired refactoring from the popup.
In the dialog that opens, specify the refactoring options.
To apply the changes immediately, depending on the refactoring type, click Refactor or OK.
For certain refactorings, there is an option of previewing the changes prior to actually performing the refactoring. In such cases the Preview button is available in the corresponding dialog.
To preview the potential changes and make the necessary adjustments, click Preview. PyCharm displays the changes that are going to be made on a dedicated tab of the Find tool window.
One of the possible actions at this step is to exclude certain entries from the refactoring. To do so, select the desired entry in the list and press Delete.
If conflicts are expected after the refactoring, PyCharm displays a dialog with a brief description of the encountered problems. If this is the case, do one of the following:
Ignore the conflicts by clicking the Continue button. As a result, the refactoring will be performed, however, this may lead to erroneous results.
Preview the conflicts by clicking the Show in View button. PyCharm shows all conflicting entries on the Conflicts tab in the Find tool window, enabling you to navigate to the problematic lines of code and to make the necessary fixes.
Cancel the refactoring and return to the editor.
When you are satisfied with the proposed results, click Do Refactor to apply the changes.
Select an item to refactor. You can select a file/folder in the Project tool window or expression/symbol in the editor.
Press Control+Alt+Shift+T to open a list of refactorings that can be selected.
Alternatively, you can use a keyboard shortcut for a specific refactoring.
You can also make a multi-caret selection for an extended range of refactoring operations, including Encapsulate Fields, Move Static Members, Extract Delegate, Extract Interface, and Extract Superclass. To utilize this feature, place the caret in each member you want to apply the refactoring to, and the IDE will automatically preselect and check these members in the refactoring dialog.
If you need to undo your refactoring, press Control+Z.
Preview refactoring changes
For some refactorings, PyCharm lets you preview the changes before applying them.
Click Preview in the Refactoring Preview dialog to see potential changes (the list of usages where the refactoring will be performed).
In the Find tool window that opens, check the changes that are going to be made. You can exclude Delete or remove Control+X changes that you consider unnecessary.
Click Do Refactor to proceed with the changes.
Configure refactoring settings
In the Settings dialog (Control+Alt+S), select .
On the Code Editing page, in the Refactorings section, adjust the refactoring options and click OK.
The most popular refactorings supported in PyCharm
Makes sure that you do not delete files that are referenced in your source code.
F5 / F6
Copies/Moves an element.
These refactoring actions help you reduce the code duplication.
Renames an element.
Inlines an element. Acts as the opposite of extracting.
Changes the call signature of a method or class.