Inline
The Inline refactoring lets you reverse the extract refactoring for a method or a variable.
Place the caret at the code fragment you want to inline.
Press Control+Alt+N. Alternatively, right-click the code fragment and go to the
menu. Select the inline refactoring that you need.In the Inline dialog, specify the inlining options.
Preview and apply changes.
Inline variable
Inline Variable refactoring replaces redundant variable usage with its initializer.
The variable must be initialized at declaration. If the initial value is modified somewhere in the code, only the occurrences before modification will be inlined.
Before | After |
---|---|
import math
class Solver:
def demo(self):
a = 3
b = 25
c = 46
# This variable will be inlined
return_type_of_sqrt = math.sqrt(b ** 2 - 4 * a * c)
root1 = (-b + return_type_of_sqrt) / (2 * a)
root2 = (-b - return_type_of_sqrt) / (2 * a)
print(root1, root2)
Solver().demo() | import math
class Solver:
def demo(self):
a = 3
b = 25
c = 46
# This variable will be inlined
root1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
root2 = (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
print(root1, root2)
Solver().demo() |
Inline method or function
Inline Method results in placing method's body into the body of its callers. When you initiate inline refactoring for a method or function, PyCharm prompts to choose whether to remove the method declaration after the refactoring or keep it intact. Depending on your choice, the refactoring results vary.
Before | After |
---|---|
def say_state(self):
print_state(self)
def print_state(self):
print("I'm going {} kph!".format(self.speed)) | def say_state(self):
print("I'm going {} kph!".format(self.speed)) |
Before | After |
---|---|
def say_state(self):
print_state(self)
def print_state(self):
print("I'm going {} kph!".format(self.speed)) | def say_state(self):
print("I'm going {} kph!".format(self.speed))
def print_state(self):
print("I'm going {} kph!".format(self.speed)) |
When PyCharm discovers several occurrence of the method or function to inline, it enables refactoring preview. Click the Preview button in the Inline Refactoring dialog to evaluate the occurrences.
Click the Do Refactor button to complete inlining.
To inline a particular invocation, place the caret at it, press Control+Alt+N, and select the Use this invocation only and keep the declaration option in the Inline Refactoring dialog.
Refactoring limitations
PyCharm doesn't not support inline refactoring for the following categories of methods and functions:
Functions that are used as a decorator
Functions that are used as a reference
Functions that use any argument unpacking
Generators
Async functions
Constructor calls
Builtin functions
Functions with decorators
Recursive functions
Functions with * arguments
Overridden functions
Functions with global variables
Functions with nonlocal variables
Functions with another function declaration
Functions that interrupt control flow
When you try to perform refactoring for any of these functions or method, the warning message will be shown: