Moving function/method to class

Viewed 3118

Using pycharm, I wish to refactor methods into a class. (Staticmethod would do) Current:

import math

class Solver(object):
   def __init__(self, a, b, c):
       self.a = a
       self.b = b
       self.c = c


def demo(b, a, c):
   d = b ** 2 - 4 * a * c
   if d >= 0:
       disc = math.sqrt(d)
       root1 = (- b + disc) / (2 * a)
       root2 = (- b - disc) / (2 * a)
       print(root1, root2)
       return root1, root2
   else:
       raise Exception


s = Solver(2, 123, 0.025)
demo(s.b, s.a, s.c)

Desired:

import math

class Solver(object):
     def __init__(self,a,b,c):
            self.a = a
            self.b = b
            self.c = c

    def demo(self, a, b, c):
            d = self.b ** 2 - 4 * self.a * self.c
            if d >= 0:
                disc = math.sqrt(d)
                root1 = (- self.b + disc) / (2 * self.a)
                root2 = (- self.b - disc) / (2 * self.a)
                print(root1, root2)
                return root1, root2
            else:
                raise Exception

Solver(2, 123, 0.025).demo()

I am basically trying to get the opposite functionality to: "Moving function/method to the top-level"

as described here: https://www.jetbrains.com/help/pycharm/2017.1/move-refactorings.html

I wouldn't mind on settling for a class with no init params.

1 Answers
Related