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

Time:11-22

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

CodePudding user response:

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.

CodePudding user response:

I believe you're asking about if you can use a class' member function where either self refers to another object which it reads its member data from (this is possible if the other object has members with the same name as your original class) OR more likely, you have a couple free-floating (not class variables) variables... or your other class does not have members with the exact same name.

In the second case, the best course of action would to be to create some stand-alone function that takes a couple parameters (or, even cleaner, a single class that holds all the arguments) and then have your classes reference this procedure, rather than having it be inside a class. The reason for this is if this behavior is required in multiple places, the class you are writing is probably not actually supposed to be responsible for it in the first place anyways.

CodePudding user response:

Here's a quick example of the syntax you need:

class MyFirstClass:
    def __init__(self, name):
        self.name = name

    @staticmethod
    def static_function():
        print("Static function called!")
    def instance_function(self):
        print("Instance function called on ", self.name)
        
        
class MySecondClass:
    def main():
        MyFirstClass.static_function()
        anInstance = MyFirstClass("Adam")
        anotherInstance = MyFirstClass("Bob")
        anInstance.instance_function()
        anotherInstance.instance_function()
        
MySecondClass.main()

CodePudding user response:

I won't speak to whether it's a good design principle (it isn't) but I have committed the sin of convenience myself. This also presupposes that the class will still be instantiated and you will be using some of the self attributes, just not all of them. If this is not the case, see other answers regarding staticmethods

class Stuff:
  def __init__() ...
  def func1(self, arg1, arg2, arg3=None, arg4=None)
    if arg3=None:
        self.var1=arg1*self.var3
    else:
        self.var1=arg1*arg3
    self.var2=arg2*self.var4

Actually there are about 10 variables that I would have to pass.

You could make use of kwargs in that case and unpack them within the function

  • Related