0

Let's say I have two classes with some methods.

First class:

@dataclass
class MyFirstClass:
    y1: List[float]
    y2: List[float]
    
    @property
    def my_func1(self) -> List[float]:
        return self.y1-self.y2

    @property
    def my_func2(self) -> List[float]:
        return self.y1+self.y2

    @property
    def my_func3(self) -> List[float]:
        return self.y1*self.y2

Second class:

@dataclass
class MySecondClass:
    list_vals: List[float]
    
    @property
    def norm1(self):
        return self.list_vals
 
    @property
    def norm2(self):
        return self.list_vals / numpy.mean(self.list_vals)

So my question is two fold. Say in my main I have something like:

def main():
    FUNCTION = <?>
    NORM = <?>

    y1 = [1, 2, 3, 4, 5]
    y2 = [3, 4, 5, 6, 7]

    test = MyFirstClass(y1, y2).my_func1

Then I just call MyFirstClass with the y1 and y2 lists and get an output.

But as can be seen in the beginning of main I have FUNCTION and NORM. Is there any way to call MyFirstClass from there, and then reuse it all through main, i.e. something like:

FUNCTION = MyFirstClass.my_func1

test = FUNCTION(y1, y2)

This doesn't work obviously. So how can one do that ?

Also, and this is probably a build upon the above, how can I, once again, choose the norm function to be used in the other class ? For instance, if I update MyFirstClass a bit to:

@dataclass
    class MyFirstClassUpdated:
        y1: List[float]
        y2: List[float]
        norm: NORM
        
        @property
        def my_func1(self) -> List[float]:
            return norm(self.y1)-norm(self.y2)
    
        @property
        def my_func2(self) -> List[float]:
            return norm(self.y1)+norm(self.y2)
    
        @property
        def my_func3(self) -> List[float]:
            return norm(self.y1)*norm(self.y2) 

And then when the class is called it takes the NORM argument from the main function, i.e. something like:

FUNCTION = MyFirstClass.func1
NORM = MySecondClass.norm1

test = FUNCTION(y1, y2, NORM)

I have no idea if this is even possible without making the code "uglier".

2
  • Is the last example supposed to use MyFirstClassUpdated? Commented Jun 20, 2022 at 21:52
  • y1 and y2 are arguments to MyFirstClass, not MyFirstClass.my_func1. The closest to what you are looking for is to make FUNCTION a bound method: FUNCTION = MyFirstClass(y1, y2).my_func1; FUNCTION(). Commented Jun 20, 2022 at 21:54

1 Answer 1

1

You need to create an instance of MyFirstClass, and pass it as the self argument to the function.

FUNCTION = MyFirstClass.my_func1
mfc = MyFirstClass(y1, y2)
test = FUNCTION(mfc)
Sign up to request clarification or add additional context in comments.

5 Comments

That seemed to do the trick. But how would I go about providing the NORM function into that class as well ?
You need to do it similarly. You have to make an instance of MySecondClass to call the NORM function on.
Where is the list_vals argument to MySecondClass in the second example?
I'm not sure I understand your last comment. In the second example the list_vals are just the self.y1 and self.y2. MySecondClass is unchanged compared to MyFirstClassUpdated. I just want to use the MySecondClass and its methods in the MyFirstClassUpdated class. That was not used in the first MyFirstClass.
Why are you trying to do this with dataclasses? It seems like you just want ordinary functions that you can pass arguments to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.