The Extract Method refactoring lets you take a code fragment that can be grouped, move it into a separated method, and replace the old code with a call to the method.
When you extract the method you need to check for variables. If there is one output variable, it is used as a return value for the extracted method. In case there are multiple output variables, the Extract Method refactoring may not be applied, and the error message appears.
There are several workarounds to allow Extract Method work in this case. For example, you may introduce a special data-class that contains all output values.
note
The Extract Method refactoring has the following limitations:
Refactoring does not work when there are references to generic types.
Refactoring does not work with multiple output values in automatic mode. You have to change your code before applying the refactoring.
tip
To reverse the Extract Method refactoring, press CtrlAlt0N to invoke the Inline refactoring.
Extract a method
In the editor, select a block of code to be transformed into a method or a function.
tip
The code fragment to form the method does not necessarily have to be a set of statements. It may also be an expression used somewhere in the code.
From the main menu or from the context menu, select Refactor | Extract | Method or press CtrlAlt0M.
In the Extract Method dialog that opens, specify the name of the new function.
In the Parameters area, do the following:
Specify the variables to be passed as method parameters, by selecting or clearing the corresponding checkboxes.
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 OK to create the required function.
The selected code fragment will be replaced with a function call.
Examples
Before
After
from enum import Enum
classCategory(Enum):
A =1
B =2
C =3defcalculate_tax(category, income):if category == Category.A:
discount =10elif category == Category.B:
discount =5else:
discount =0return income *(100- discount)/100
from enum import Enum
classCategory(Enum):
A =1
B =2
C =3defcalculate_tax(category, income):
discount = cacl_discount(category)return income *(100- discount)/100defcacl_discount(category):if category == Category.A:
discount =10elif category == Category.B:
discount =5else:
discount =0return discount
Processing duplicates
If duplicate code fragments are encountered, PyCharm suggests to replace them with the calls to the extracted method:
Thanks for your feedback!
Was this page helpful?
Cookie Settings
Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions. Learn more.
With your consent, JetBrains may also use cookies and your IP address to collect individual statistics and provide you with personalized offers and ads subject to the Privacy Notice and the Terms of Use. JetBrains may use third-party services for this purpose. You can adjust or withdraw your consent at any time by visiting the Opt-Out page.