PyCharm Refactoring Tutorial
What this tutorial is about
This tutorial shows some refactorings available in PyCharm, using the example of a simple class that makes use of the rational numbers.
Make sure that the following prerequisites are met:
You are working with PyCharm version 2016.2 or later.
A project is already created.
Preparing an example
Create a Python file rational.py in your project and add the following code:
Simplifying rational number
Let's simplify a rational number by dividing numerator and denominator by the greatest common divisor:
Extracting a method
Now, let's extract the search for a greatest common divisor to a separate method. To do that, select the statements
and press Ctrl+Alt+M. In the dialog box that opens type the method name (
gcd) and then click OK:
Inlining a local variable and changing method signature
Let's get rid of the variable
factor, by using Inline variable refactoring. To do that, place the caret at the variable and press Ctrl+Alt+N. All the detected
factor variables are inlined.
Next, change the parameter names using Change signature. To do that, place the caret at the method declaration line and press Ctrl+F6. In the dialog that opens, rename the parameters
y respectively, and click to change the order of parameters.
You end up with the following code:
Using quick fix
Now, let's convert the existing static method to a function. To do that, press Alt+Enter, from the suggestion list choose Convert static method to function and press Enter:
Moving the function to another file
Now, we'll move the function to a separate file and add an import statement. To do that, place the caret at the function
gcd declaration and press F6. In the dialog that opens specify the fully qualified path of the destination file util.py. This file does not exist, but it is created automatically:
The import statement is also added automatically. Thus the file rational.py looks as follows:
Further changes of the class Rational
Adding magic methods
Next, let us add declarations of the magic methods for the addition/subtraction operations on the objects of the class
Extracting method and using a quick fix
Next, we'll extract an expression
Rational(other, 1) into a separate method. To do that, place the caret at the aforementioned expression, press Ctrl+Alt+M and in the dialog box that opens, type the new method name
Finally, place the caret at the method
from_int declaration, press Alt+Enter, select Make method static from the suggestion list, and then press Enter:
Finally, let's change the name of the parameter
number. To do that, place the caret on the parameter and press Shift+F6.
Extracting a superclass
Next, we'll move the implementations of the methods
__rsub__ into a superclass. Also, we'll make the methods
This is how it's done... Place the caret at the class
Rational declaration, on the context menu point to and choose . Next, in the dialog box that opens, specify the name of the superclass (here it's
AdditiveMixin) and select the methods to be added to the superclass. For the methods
__add__, select the checkboxes in the column Make abstract.
End up with the following code: