首页 > 解决方案 > 向内置模块添加属性需要什么?

问题描述

查看 NumPy 源代码,我在文件中看到了一个奇怪的声明setup.py

# This is a bit hackish: we are setting a global variable so that the main
# numpy __init__ can detect if it is being loaded by the setup routine, to
# avoid attempting to load components that aren't built yet.  While ugly, it's
# a lot more robust than what was previously being used.
builtins.__NUMPY_SETUP__ = True

我知道这是一种声明常量的方法,以便可以从主 NumPy init 访问它。但是使这项工作发挥作用的机制是什么?查看NumPy 的主 init 文件,您可以看到它认为__NUMPY_SETUP__它是可访问的,即使该文件没有导入builtins模块。

标签: pythonpython-3.xnumpy

解决方案


推荐阅读