1

I have a base class called customclass that dynamically loads a 2nd class and calls methods on it. I use the 2nd answer here: How to dynamically load a Python class to implement this, the method that dynamically loads the 2nd class in my customclass follows:

def executedf2in(self, dfin1, dfin2):
    custom_class = locate(self.custom_class_name)
    dfresult = custom_class.df2in(dfin1, dfin2, self.custom_params)

Here is the custom_class code:

class CustomWork(object):

    def df2in(self, df1, df2, custom_params):
        dfchanged = pd.concat([df1, df2], sort=True)

        return dfchanged

I get this error:

File "/home/david/git/testapp/app/etl/customclass.py", line 54, in executedf2in
dfresult = custom_class.df2in(df1, df2, self.custom_params)
AttributeError: 'module' object has no attribute 'df2in'

When I inspect the CustomWork instance created it shows under Special Variables in the inspect window, it shows no functions under the CustomWork class

<module 'app.test.custom_work_test' from '/home/david/git/testapp/app/test/custom_work_test.py'>
    <class 'app.test.custom_work_test.CustomWork'>

How do I get it to load the class dynamically with the functions defined therein?

oldDave
  • 395
  • 6
  • 25

2 Answers2

2

In the end I found locate could not instantiate a class with function, just the class is instantiated. So I converted the class I wanted to load dynamically to a simple module and then the functions were instantiated and executable.

oldDave
  • 395
  • 6
  • 25
0

The problem is that locate() returns the type. You need to instantiate it to call the method. If the method was static, you could call it as written. Otherwise you just need an instance:

custom_class = locate(self.custom_class_name)
custom_instance = custom_class()
dfresult = custom_instance.df2in(dfin1, dfin2, self.custom_params)
Ron P
  • 1