PyCharm 2016.2 Help

Make Top-Level Function

In this section:

Overview

This refactoring moves local functions or methods to the top level and converts their variables to input parameters and return values. So doing, the access to the attributes and return values is converted, and the usages are updated.

Moving a local function or a method to the top level

To move a function or a method to a top level, follow these steps:

  1. Place the caret at the local function or method name.
  2. On the main menu, or on the context menu of the editor, choose Refactor | Make Top-Level Function.
    The preview shows in the Find tool window.
  3. If satisfied with the preview results, confirm move by clicking Do Refactor

Example

BeforeAfter
import math class Solver: def __init__(self, a, b, c): self.a, self.b, self.c = a, b, c def demo(self): d = self.b ** 2 - 4 * self.a * self.c if d < 0: raise ValueError('Complex roots') disc = math.sqrt(d) root1 = (-self.b + disc) / (2 * self.a) root2 = (-self.b - disc) / (2 * self.a) return root1, root2 print(Solver(2, -30, 100).demo())
import math class Solver: def __init__(self, a, b, c): self.a, self.b, self.c = a, b, c def demo(b, a, c): d = b ** 2 - 4 * a * c if d < 0: raise ValueError('Complex roots') disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 solver = Solver(2, -30, 100) print(demo(solver.b, solver.a, solver.c))
Last modified: 29 August 2016