Search code examples
pythonfunctionclassobjectoop

python: is there any practice to allow using class functions outside of the object


I just want to say I am a newbie to OOP so I am not sure what I am supposed to do there. so lets say I have a class that has a whole bunch of functions on data in it:

class stuff:
  def __init__ ...
  def func1(self, arg1, arg2)
    self.var1=arg1*self.var3
    self.var2=arg2*self.var4
  ...

the func1 uses a lot of variables from the class (using self), and I have a lot of functions and a lot of variables which is very convenient in a class. However every once in a while I also need to use the function on data outside of an object. In that case I would have to pass var3 and var4 to the function and I don't know how to do that. Actually there are about 10 variables that I would have to pass.

So is there a good way to do that? Should I make a copy of every function for using outside of objects? But there are a lot of functions and they are quite long, and I will have to remove self. before every variable, it will be hard to maintain.

Should I create an object for all data I want to process? That would also be annoying, init does a lot of stuff that requires full data and moving it to separate functions will create a lot of them.

Should I make functions inside class that just call functions outside class? Two issues, I would have to name them differently and memorize both names, and what if the data I am passing is very big? Or is there a different way to do that?

So I was wondering if there is something to fix that in python because I don't know what to google. I've noticed a lot of libraries use "." in their function names so I assume those are in functions in classes, but I seem to use them on my data, without creating objects


Solution

  • the func1 uses a lot of variables from the class (using self), and I have a lot of functions and a lot of variables which is very convenient in a class

    this is a clear violation of the single responsibility principle, so you should consider splitting the class down to multiple classes, each one is responsible for one kind of input data and handles operations on it.