Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic method ?
#1
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 ?
Reply
#2
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
Reply
#3
(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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import Module and Method ( dynamic ) ? JohnnyCoffee 0 1,492 Mar-23-2020, 04:23 PM
Last Post: JohnnyCoffee
  Method for pulling in dynamic table data brocq_18 1 3,292 Apr-03-2017, 04:07 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020