1

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

stunlocked
  • 181
  • 10
  • Yes, definitely. You just need to reference it by the name of the class if it's a static function (one that lacks the `self` parameter), or using an instance if it's an instance method (i.e., non-static.) So, either `nameOfClass.foo()` or `instanceOfClass.foo()`. – Edward Peters Nov 21 '22 at 17:58
  • @Edward Peters static functions need `@staticmethod` – cards Nov 21 '22 at 18:00
  • Does static function just mean something different in python? I just know it as "Function that's not on an instance", and it doesn't require any sort of annotation to do that... – Edward Peters Nov 21 '22 at 18:02
  • @Edward Peters with that definition also a class-method would be static – cards Nov 21 '22 at 18:05
  • @cards Check the answer I posted - is that not a static method, to you? It would qualify as one in java lingo, and a quick search doesn't seem to indicate that python uses the term differently.. – Edward Peters Nov 21 '22 at 18:07
  • @Edward Peters [check the comments of the answer](https://stackoverflow.com/questions/74353515/how-to-get-the-name-of-the-class-a-function-is-in-with-python-3-11/74353542#comment131265205_74353542) – cards Nov 21 '22 at 18:12
  • Huh, swear I haven't run into that before, but I don't do much python. I guess it's only needed if you want to call the static method from an instance? – Edward Peters Nov 21 '22 at 18:18

4 Answers4

1

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.

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • This answer is absolutely true and good advice, but I think - well, I won't use their name, I don't want to be mean - is still learning how to call functions and create instances :) – Edward Peters Nov 21 '22 at 18:01
  • 1
    @EdwardPeters someone had to put this here, i just volunteered :D – Ahmed AEK Nov 21 '22 at 18:02
0

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.

AlgoRythm
  • 1,196
  • 10
  • 31
0

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()
Edward Peters
  • 3,623
  • 2
  • 16
  • 39
0

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

G. Anderson
  • 5,815
  • 2
  • 14
  • 21