小知识点1

通过读取配置文件执行相应的方法

文件路径

# settings.py 模块是用来配置相应的路径

setting_list = [
    'test1.message.email.Email',
    'test1.message.message.Message'
]
#run.py 模块是调用send()方法

from test1.message import send

def fun(msg):
    send(msg)

msg = "hello world"
fun(msg)
# __init__.py 模块加载send()方法时,从settings中找到配置文件
#加载模块,创建对象,执行配置文件中所对应的方法

import importlib
from settings import setting_list

def send(msg): for path in setting_list: m,c = path.rsplit('.',1) md = importlib.import_module(m) cls = getattr(md,c)() cls.send(msg)
# email.py 执行email发送

class Email():
    def send(self,msg):
        print('email'+msg)

 

posted @ 2018-09-21 14:50  努力吧,少年  阅读(148)  评论(0编辑  收藏  举报