personal web log written by izabeera and dryobates

rope refactoring

Refactoring - moving objects between modules

by dryobates

Are you scared when you have to move classes or functions between modules? You’re tired by fixing all those references to your class? With Rope - python refactoring library - it won’t be nightmare anymore.

From time to time every developer has to do refactoring. In some methodologies like eXtreme Programming it’s part of the process and is done quite easily. XP encourages doing often refactoring and with good coverage with tests you know when your changes has broken the code.

For python there’s refactoring library Rope [1]. I use it with Vim [2]

In order to start using rope with current project, open some file from that project and run:


It can take some time with large projects. When it finished we can start using Rope to make refactoring.

I had a Django project which had forms mixed in module. We prefer keep forms in their own modules. I could cut it from and paste in but the I had to replace all places in code where are references to that form. It could be tedious. With Rope it was as simple as putting cursor on class, that I want move and run:


And then answer where I want to put it (“package.forms”) and confirm by pressing enter. If you want to check what would be changed instead of performing change, just select “preview” option. You will be presented with unified diff.

Rope is smart. When you move class to other module it not only changes references to that class in other modules, it also puts required imports in new module.

After refactoring it is wise to run tests. So do it and enjoy saved time :)

[1]Rope - python refactoring library:
[2]Ropevim - Vim plugin utilising Rope
Jakub Stolarski. Software engineer. I work professionally as programmer since 2005. Speeding up software development with Test Driven Development, task automation and optimization for performance are things that focus my mind from my early career up to now. If you ask me for my religion: Python, Vim and FreeBSD are my trinity ;) Email:


Tag cloud