PyCharm Edu 2019.2 Help

Change signature

The Change Signature refactoring combines several different modifications that can be applied to a function signature. You can use this refactoring to:

  • change the function name

  • add new parameters and remove the existing ones

  • To assign default values to the parameters

  • reorder parameters

When changing a function signature, PyCharm Edu searches for all usages of the function and updates all the calls, implementations, and override replacements of the function that can be safely modified to reflect the change.

Examples

Before

After

# This function will be renamed: def fibonacci(n): a, b = 0, 1 while b < n: print(b) a, b = b, a+b n = int(input("n = ")) fibonacci(n)
# Function with the new name: def fibonacci_numbers(n): a, b = 0, 1 while b < n: print(b) a, b = b, a+b n = int(input("n = ")) fibonacci_numbers(n)

Default value and propagation of new parameters

For each new parameter added to a function, you can specify:

  • A default value (or an expression) (the Default value field).

You can also propagate the parameters you have introduced to the functions that call the function whose signature you are changing.

The refactoring result depends on whether you specify the default value and whether you use propagation.

Propagation. New parameters can be propagated to any function that calls the function whose signature you are changing. In such case, generally, the signatures of the calling functions change accordingly. These changes, however, also depend on the default values set for the new parameters.

Default value. Generally, this is the value to be added to the function calls.

If the new parameter is not propagated to a calling function, the calls within such function will also use this value.

If propagation is used, this value won't affect the function calls within the calling functions.

More refactoring examples

To see how different refactoring settings discussed above affect the refactoring result, let us consider the following examples.

All the examples are a simplified version of the refactoring shown earlier. In all cases, a new parameter wireframe1 of the type Boolean is added to the function paint() defined in the IShape interface.

In different examples, different combinations of the initializer and the default value are used, and the new parameter is either propagated to Canvas.paint() (which calls IShape.paint()) or not.

Initializer

Default value

Propagation used

Result

false

Yes

public interface IShape { function paint(g:Graphics, wireframe:Boolean):void; } // The function paint() in the Canvas class: public function paint(g:Graphics, wireframe:Boolean): void { for each ( var shape: IShape in shapes) { shape.paint(g,wireframe); } }

false

No

public interface IShape { function paint(g:Graphics, wireframe:Boolean):void; } // The function paint() in the Canvas class: public function paint(g:Graphics): void { for each ( var shape: IShape in shapes) { shape.paint(g,false); } }

true

Yes

public interface IShape { function paint(g:Graphics, wireframe:Boolean = true):void; } // The function paint() in the Canvas class: public function paint(g:Graphics): void { for each ( var shape: IShape in shapes) { shape.paint(g); } }

true

No

public interface IShape { function paint(g:Graphics, wireframe:Boolean = true):void; } // The function paint() in the Canvas class: public function paint(g:Graphics):void { for each ( var shape: IShape in shapes) { shape.paint(g); } }

true

false

Yes

public interface IShape { function paint(g:Graphics, wireframe:Boolean = true):void; } // The function paint() in the Canvas class: public function paint(g:Graphics, wireframe:Boolean = true): void { for each ( var shape: IShape in shapes) { shape.paint(g,wireframe); } }

true

false

No

public interface IShape { function paint(g:Graphics, wireframe:Boolean = true):void; } // The function paint() in the Canvas class: public function paint(g:Graphics): void { for each ( var shape: IShape in shapes) { shape.paint(g,false); } }

Changing a function signature

Default value and propagation of new parameters

  1. Place the caret at the name of the function that you want to refactor.

  2. Press Ctrl+F6. Alternatively, select Refactor | Change Signature from the main menu or from the context menu.

  3. In the Change Signature dialog, make necessary changes to the function signature depending on your needs:

    • Change the function name. To change the name, edit the text in the Name field.

    • Manage the function parameters. To configure the parameters, use the table and the buttons in the Parameters area:

      • To add a new parameter, click The Add button and specify properties of the new parameter in the corresponding table row.

      • To remove a parameter, select any row and click The Remove button.

      • To reorder the parameters, use the Up (The Up icon) and Down (The down icon) icons. For example, if you want to put a certain parameter first in the list, click any of the cells in the row corresponding to that parameter, and then click The Up icon the required number of times.

      • To change the name or the default value of a parameter, make the necessary updates in the table of parameters (in the fields Name and Default value respectively).

  4. Click Refactor.

  5. To see the expected changes and make adjustments prior to the refactoring, click Preview.

Last modified: 22 January 2020