Extract Parameter
The Extract Parameter refactoring is used to add a new parameter to a function declaration and to update the function calls accordingly.
- Example
- Extracting a parameter in Python in-place
- Extracting a parameter in Python using the Extract Parameter dialog
See also Change Signature.
Example
Before | After |
---|---|
def print_test(self):
print "test" |
def print_test(self,test):
print test |
![]() | ![]() |
Extracting a parameter in Python in-place
The in-place refactorings are enabled in PyCharm by default. So, if you haven't changed this setting, the Extract Parameter refactorings for Python are performed in-place, right in the editor:
- In the editor, place the cursor within the expression to be replaced by a parameter.
- Do one of the following:
- 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 Enter to select it.
- Type the parameter name in the box with a red border.
-
To complete the refactoring, press
Tab
or
Enter.
If you haven't completed the refactoring and want to cancel the changes you have made, press Escape.
Note that sometimes you may need to press the corresponding key more than once.
Extracting a parameter in Python using the Extract Parameter dialog
To be able to use the Extract Parameter dialog (instead of performing the refactoring in-place), make sure that the Enable in place refactorings option is off in the editor settings.
Once this is the case, you perform the Extract Parameter refactoring as follows:
- In the editor, place the cursor within the expression to be replaced by a parameter.
- Do one of the following:
- 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 Enter to select it.
- In the
Extract Parameter dialog that opens:
- Specify the parameter name in the Name field.
- If more than one occurrence of the expression is found within the function body, you can choose to replace only the selected occurrence or all the found occurrences with the references to the new parameter. Use the Replace all occurrences check box to specify your intention.
- Preview and apply changes.