Wing Tips: Renaming Symbols and Attributes in Python Code with Wing Pro's Refactoring Tool

May 09, 2019

In the previous Wing Tips post we looked at using multiple selections to edit several parts of code at once. As part of that, we briefly mentioned that refactoring is a better approach when renaming a symbol or attribute globally. Let's take a closer look at that now.

What is Refactoring Anyway?

Refactoring is the process of changing code in a way that does not alter its functionality, in order to better organize the code or make it easier to read and maintain. A round of refactoring is often appropriate before working on code that has become a bit crufty over time.

IDEs like Wing Pro can help with this process by automating some of the operations commonly made during refactoring, including renaming symbols or attributes, moving symbols around, collecting code into a new function or method, and so forth.

Renaming Symbols and Attributes

Rename refactoring is often used to make code more readable by selecting clearer or more appropriate names. It may also be used to change a method on a class from __Private form, which in Python can only be accessed from code in the class itself, to a form that can be called from code outside of the class. For example:


Renaming method "__SetPosition" to "_SetPosition" with refactoring, so it can be used from outside of the class

Renaming Modules and Packages

Rename refactoring may also be used on whole modules or packages, by renaming any use of the module or package name. Wing Pro will rename the associated disk files and directories and track the change in the active revision control system, if any.


Renaming module "urlutils" to "urlops" with refactoring

Like-Named Symbols and Symbol Identity

Wing Pro's rename refactoring uses static source analysis of your code to determine which symbols are actually the same symbol. For example, in the following code there are two distinct symbols called name, one in the scope show_name and another in the scope process_name:

def show_name(name=None):
    if name is not None:

def process_name(name):
    show = enter_name(name)
    if show:

Renaming name in the first function should only affect that scope, and any code that is passing the argument by name, as in the following example:


Refactoring to rename only one of two distinct but like-named symbols "name"

Uncertain Symbol Identity

In some cases, Wing Pro cannot determine for certain that a like-named symbol is actually the same symbol as the one you are renaming. In the following example, a missing import statement prevents Wing from determining that the instance of name in the file is definitely the same symbol:


Renaming "name" finds an uncertain match, where a missing import prevents analysis from establishing the symbol's identity

When this occurs, Wing marks the potential match with a ? and won't rename it unless you check the checkbox next to it. Items can be visited in the editor by clicking on them in the Refactoring tool.

If you find Wing is failing to identify many symbols with certainty, you may want to check that your configured Python Path in Project Properties is allowing Wing to trace down the modules that you import in your code. You should see code warning indicators on imports that cannot be resolved.

In some other cases, adding type hints may also help Wing's static analysis of your code.

Wing Pro also provides a number of other refactoring operations that we'll eventually go through here in Wing Tips. For more information, take a look at Refactoring in the product manual.

That's it for now! We'll be back next week with more Wing Tips for Wing Python IDE.

Share this article: