python - 为什么Python 3统计模块有collections模块作为子模块?

标签 python statistics python-3.4

检查 statistics 模块时,Python 将 collections 模块列为其子模块之一:

>>> import statistics
>>> 'collections' in dir(statistics)
True

此外,collections 子模块实际上是标准的 Python collections 模块:

>>> import collections
>>> collections == statistics.collections
True

为什么会出现这种行为?

最佳答案

它没有任何子模块。 集合导入。导入模块会将对其的引用添加到全局命名空间。来自 statistics module source :

import collections

模块也是对象,存储在 sys.modules 结构中,模块全局中的名称只是对这些对象的引用。因此,不仅 collections == stats.collections 为 true,collections is sys.modules['collections'] 也为 true。

关于python - 为什么Python 3统计模块有collections模块作为子模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031717/

相关文章:

python - 缩进错误: expected an indented block, python(如果/否则)

vba - Excel 中的 RAND() 函数用于蒙特卡罗模拟有多好?

c# - 从数据绘制具有正态分布叠加的直方图

python - Python 3.4 中的 BeautifulSoup 语法无效(2to3.py 之后)

python-requests - 无法使用 Python3 模块请求 POST 到 Grafana

Python 搜索和替换是否重复替换字符串?

python - 在 Flask Admin 中用富文本编辑器替换 textarea

python - 使用 Tkinter 进行子类化

algorithm - 为什么我们说 map-reduce 比传统方法更好地解决了 "Paper reference"问题?

python-3.4 - 我该如何克服这个关键词错误?