1

I'm in midst of refactoring a large set of python files and merging various methods into classes for modularity.

However, the use of self everywhere is quite daunting. It's impossible to change every variable reference using regexes since it's context sensitive. Is there a way to speed it up (or not add self everywhere)?

For example:

Before refactoring

def my_func():
  W = P + Kp + S + M + R

Each of the variables above is needed by multiple methods and hence I decided to put them as data members of the class. So now, it's self.W, self.P, ...

After Refactoring

class classifedData: #no pun intended
 def __init__(self, data):
   self.W = data['W']
   self.P = data['P'] #and so on

 def my_func():
   self.W = self.P + self.Kp + self.S + self.M + self.R #too much self?

However, this requires a major overhead for making changes across huge files.

Questions:

  1. Is there an easier way? Can the self somehow be 'scoped' with with to make this easier?
  2. Or is converting this to classes a bad idea and the overhead is what I must bear OR just refactor it as well structured procedural code? I.e., simulating a class like organization at the module level.

I'm fairly new to python and am working on some scientific/math intensive code base that needs to be refactored for maintainability and am not sure how to handle/incorporate the reference to self at numerous locations?

PhD
  • 11,202
  • 14
  • 64
  • 112

0 Answers0