Class inheritance vs passing class object as argument?

Viewed 35

there! In a project I'm working on, we have a class that stores some DataFrames that are key for the project, that will be initialized and set up with a method of the class. There will also be other methods that deal with those DataFrames.

I'm in the need of reading and modifying those DataFrames from other classes and functions. The question is, which of the following approaches is better or more convinent?

1. Passing the instance of the class as an argument in the method of class that reads/writes those DataFrames

class SomeClass:
    def __init__(self, df):
        self.df = df
        self.mod_df = df

    def some_method(self, parameter):
        # do some operations with any of the df

class OtherClass:
    def __init__(self):
        pass

    def some_operation(self, someclass_obj, param1, param2):
        someclass_obj.some_method(param1)
        someclass_obj.some_method(param2)

2. Passing the instance of the class as an argument in the init of class that reads/writes those DataFrames

class SomeClass:
    def __init__(self, df):
        self.df = df
        self.mod_df = df

    def some_method(self, parameter):
        # do some operations with any of the df

class OtherClass:
    def __init__(self, someclass_obj):
       pass

    def some_operation(self, param1, param2):
        someclass_obj.some_method(param1)
        someclass_obj.some_method(param2)

3. Creating a child class of the first class that shares state with parent class, suign Borg pattern

class SomeClass:
    __shared_state = {}

    def __init__(self, df):
        self.__dict__ = self.__shared_state
        self.df = df
        self.mod_df = df

    def some_method(self, parameter):
        # do some operations with any of the df

class OtherClass:
    def __init__(self):
       super().__init__(df)

    def some_operation(self, param1, param2):
        self.some_method(param1)
        self.some_method(param2)

I'm inclined to take the first or second approach. I don't see if approach 3 has some drawback that I'm not able to forsee.

Thanks! (First time posting, if anything's out of common usage, let me know!)

0 Answers
Related