image.png
    init.py

    1. import settings
    2. import importlib
    3. def send_all(content):
    4. for path_str in settings.NOTIFY_FILES_DIR: #循环配置文件内字符串
    5. module_path,class_name = path_str.rsplit('.',maxsplit=1) #切片分开类名与模块路径
    6. module = importlib.import_module(module_path) #导入模块
    7. cls = getattr(module,class_name) #拿到类的名字
    8. obj = cls()
    9. obj.send(content)

    email.py

    1. class Email(object):
    2. def __init__(self):
    3. pass
    4. def send(self,content):
    5. print('邮箱通知:%s'%content)

    wechat.py

    1. class Wechat():
    2. def __init__(self):
    3. pass
    4. def send(self, content):
    5. print('微信通知:%s' % content)

    run.py

    1. import NOTIFY
    2. NOTIFY.send_all('真尼玛难学')

    settings.py

    1. NOTIFY_FILES_DIR = [
    2. 'NOTIFY.email.Email',
    3. 'NOTIFY.wechat.Wechat',
    4. ]