11

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?

1 Answer 1

8

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".

"Convert method to property" intention

You can also trigger the global action search with Ctrl+Shift+A and select the intention from there.

Alternative

Sign up to request clarification or add additional context in comments.

1 Comment

Strangely, the inverse operation isn't available :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.