将名称附加到许多__init __ *。py中如何工作?

zuz*_*zuz 7 python init

我正在分析其中有两个类似init的文件的python代码。名称为__init__solvers.py__init__cases.py,它们包含典型的__init__.py结构和初始化数据。例如,__init__solvers.py包含以下内容:

# List of solvers
solvers = ["s1", "s2", "s3"]

# Wrapper for solvers
def Solver(choice, options):
    "Return solver instance for given choice"
    exec("from %s import Solver as ChosenSolver" % choice)
    return ChosenSolver(options)
Run Code Online (Sandbox Code Playgroud)

主模块按如下方式导入求解器和求解器:

from solvers import Solver, solvers
Run Code Online (Sandbox Code Playgroud)

对的内容执行了类似的方法__init__cases.py。它是为Python 2.x编写的。目录结构如下所示:

main/
  __init__.py
  mainmodule.py
  solversandcases/
    __init__solvers.py
    __init__cases.py
    basicsolver.py # where BasicSolver() class is defined
    basiccase.py # where BasicCase() class is defined
    s1.py # where Solver(BasicSolver) class is defined
    c1.py # where Case(BasicCase) class is defined
Run Code Online (Sandbox Code Playgroud)

该代码使用以下命令执行:

python2 mainmodule.py s1 c1
Run Code Online (Sandbox Code Playgroud)

其中s1是求解器的选择,“ c1”是要运行的情况的选择。无例外运行并产生预期的输出。我只是不知道这是如何工作的,在官方文档中也没有任何关于将名称附加到__init__.py文件名的内容。有人可以解释吗?