3

Some libraries in Python are organized as a hierarchy of submodules. What are the pros and cons of such an design from the of view of the user of the library?

For instance:

from scipy.sparse.csgraph import dijkstra

versus:

from scipy.csgraph import dijkstra

or even an entirely flat design:

from scipy import dijkstra

Note:

  1. This question is specifically about Python, because idiomatic Python makes other tradeoffs than say idiomatic Java.
  2. I am talking about the user interface, not how you should structure your code as a library designer.
4

1 回答 1

0

我认为Python包中特定于接口客观方面将至少包含以下内容:

  • 名称间距
    如果您的包中有多个实现,显然将它们放在不同的子模块中以避免像ordijkstra这样的奇怪名称是有意义的。通过将每个实现放在一个单独的模块中,这永远不会发生冲突。miller_dijkstradijkstra_v1

  • 前向声明
    在某些情况下,如果你有循环导入,你会在 Python 中遇到问题。然后将所有内容放在单独的模块中有时可以帮助解决这些问题。

于 2013-11-06T09:59:39.633 回答