Importing a python module without actually executing it

后端 未结 6 2041
抹茶落季
抹茶落季 2020-12-10 01:48

In the context of a complex application, I need to import user-supplied \'scripts\'. Ideally, a script would have

def init():
    blah

def execute():
    mo         


        
6条回答
  •  情歌与酒
    2020-12-10 02:20

    A solution to 1 to 3, ( not the yadda part ) is to hand out "generic_class.py" with all the methods that you need. So,

    class Generic(object):
    
        def __init__(self):
            return
    
        def execute(self):
            return
    
        # etc
    

    You can then check for the existence of "generic" in what you've imported. If it doesn't exist you can ignore it and if it does then you know exactly what's there. Anything extra will never be called unless it's called from within one of your pre-defined methods.

提交回复
热议问题