Pull members up, push members down
The Pull Members Up refactoring allows you to move class members to a superclass.
Pull Members Up refactoring can create abstract methods. If a project makes use of the interpreter Python 2.x, then only the instance methods can be abstracted. If a project uses Python 3.x, then any method can be abstracted.
Note that PyCharm automatically adds import statements, required for abstract methods.
The Push Members Down refactoring helps clean up the class hierarchy by moving class members to a subclass. The members are then relocated into the direct subclasses only.
Pull members up
Select the class to be moved to a superclass.
From the main or context menu, call Pull Members Up dialog appears.
. TheSelect the destination object (superclass).
In the Members section, select the members you want to move.
To move a method as abstract, select the checkbox in the column Make abstract next to the method.
Click Refactor to pull the selected members to their destination.
Before | After |
---|---|
class SuperClass:
def super_method(self):
pass
class SubClassOne(SuperClass):
def my_method(self):
pass | class SuperClass:
def super_method(self):
pass
def my_method(self):
pass
class SubClassOne(SuperClass):
pass |
Push members down
In the editor, open the class whose members you need to push down.
From the main or context menu, choose Push Members Down dialog displays the list of members to be pushed down.
.In the Members to be pushed down area, select the members you want to move. Note that the member at caret is already selected.
If pushing a member might cause problems, you will be notified with red highlighting. It means that, if the situation is unattended, an error will emerge after refactoring. PyCharm prompts you with a Problems Detected dialog, where you can opt to ignore or fix the problem.
Preview and apply changes.
Before | After |
---|---|
class SuperClass:
def super_method(self):
pass
class SubClassOne(SuperClass):
def my_method(self):
pass
class SubClassTwo(SuperClass):
def my_method(self):
pass | class SuperClass:
pass
class SubClassOne(SuperClass):
def my_method(self):
pass
def super_method(self):
pass
class SubClassTwo(SuperClass):
def my_method(self):
pass
def super_method(self):
pass |