我正在使用 PyCharm 并且我正在嘗試進行一些重構,但不知道我如何能夠以快速可靠的方式完成此操作。
我有一個方法做太多事情,我想將一部分提取到另一種方法中。提取的方法不應在提取的方法中呼叫,而應在呼叫方法中呼叫。
當前狀態
class User():
def doStuff(self):
calculateA()
calculateB()
calculateC()
def callerA():
# do other things before
obj.doStuff()
def callerAA:
# do other things before
obj.doStuff()
#... and many more methods calling doStuff method
通緝
class User():
def doStuff(self):
calculateA()
def doOtherStuff(self):
calculateB()
calculateC()
def callerA():
obj.doStuff()
obj.doOtherStuff()
def callerAA:
obj.doStuff()
obj.doOtherStuff()
#... and many more methods calling doStuff method and doOtherStuff
# Now I'll be able to create this new method only doing a subset of the original method
def aNewMethod:
obj.doStuff()
這可能與 PyCharm 有關嗎?一直在玩重構而沒有任何運氣。提取到方法中是我認為最簡單的部分,但方法呼叫最終會出現在錯誤的地方。如果在 Intellij 中有可能,我也有許可證,所以我可以切換。
uj5u.com熱心網友回復:
沒有這樣的選擇。歡迎您在 https://youtrack.jetbrains.com/issues/PY 提交功能請求有關如何使用 YouTrack 的資訊:https://intellij-support.jetbrains.com/hc/en-us/articles/207241135 -How-to-follow-YouTrack-issues-and-receive-notifications
uj5u.com熱心網友回復:
步驟 1:將兩個新方法提取到您的 User 類中
這個:
class User():
def doStuff(self):
calculateA()
calculateB()
calculateC()
變成:
class User():
def doStuff(self):
newMethodA()
newMethodB()
def newMethodA(self):
calculateA()
def newMethodB(self):
calculateB()
calculateC()
第 2 步:行內 doStuff 方法
class User():
def newMethodA(self):
calculateA()
def newMethodB(self):
calculateB()
calculateC()
def callerA():
newMethodA()
newMethodB()
def callerAA():
newMethodA()
newMethodB()
第 3 步:重命名方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/326765.html
