Python Forum

Full Version: Dynamic method ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to access a class method (moduleB) by returning a class A method. This I can do by importing the native module to receive the module name as a parameter:
    importlib.import_module(moduleB)
Now I need to get the parameter of methodB and give functionality in the return:
    params = "methodB()"
    return ModuleB.params
Does the example above generate an error ?
Please give us something to work with.
I don't understand why the import wouldn't be just import moduleB
Please show more code, preferably enough so that it can be run
(Mar-20-2020, 07:10 AM)Larz60+ Wrote: [ -> ]Please give us something to work with.
I don't understand why the import wouldn't be just import moduleB
Please show more code, preferably enough so that it can be run

The module (importlib.import_module (parameter)) is ideal for receiving modules as a parameter in order to streamline the import in just a few lines of code, imagine a case in which you need to import modules based on the parameter received in a function ?
import importlib

def dynamic_data (any_module, any_method)
    
     module = importlib.import_module (any_module)
     output = module .__ name__
     print (output)
     any_module
Now my doubt would be how do I return with any_modulo and any_method, I imagined something like, but it doesn't work:

return any_module.any_method()