Importing modules inside python class

前端 未结 4 1672
孤独总比滥情好
孤独总比滥情好 2020-12-23 09:11

I\'m currently writing a class that needs os, stat and some others.

What\'s the best way to import these modules in my class?

I\'m

4条回答
  •  旧时难觅i
    2020-12-23 09:28

    import sys
    from importlib import import_module
    
    class Foo():
    
        def __init__(self):
    
            if self.condition:
                self.importedModule = import_module('moduleName')
    
            if 'moduleName' in sys.modules:
                self.importedModule.callFunction(params)
    
            #or
    
            if self.condition:
                self.importedModule.callFunction(params)
    

提交回复
热议问题