I have the following class:
class Dogs(object):
def __init__(self):
self.names = []
self.breeds = set()
def number(self):
return len(self.names)
I want to change number
to being a property. This means I also want to change all of it usages. Does PyCharm have this built into it's refactoring tools? It seems to be according to this issue.
At this point, I'm doing "find all usages" and then manually fixing each instance. If there isn't a specific refactoring tool for changing a method to a property, is there some way to use "find all usages" more effectively?
Press Ctrl+Shift+R and then choose Convert Method to Property.
Refactoring means organizing your code without modifying its original functionality. Refactoring is the process of making small changes or adjustments to your code without affecting or changing how the code functions while in use.
From the main menu or from the context menu, select Refactor | Extract | Method or press Ctrl+Alt+M . In the Extract Method dialog that opens, specify the name of the new function.
Yes, you can refactor a method to a property in PyCharm. Strangely, this does not correspond to any entry in the "Refactor" menu.
Just place the editor cursor on the name of the method and press Alt+⏎ shortcut to show the available Intentions.
Then, select "Convert method to property".
You can also trigger the global action search with Ctrl+Shift+A and select the intention from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With