python - 向内置模块添加属性需要什么?
问题描述
查看 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
模块。
解决方案
推荐阅读
- php - 文件存在时调用 null Laravel 上的成员函数 move()
- javascript - 返回带有承诺的对象(解决,拒绝)
- android - 在 Android 中连接 wamp 服务器
- javascript - 即使我有这个角色,仍然会发送失败消息
- node.js - 当所有测试成功或失败时,有没有办法向 Jest 添加回调?
- php - Composer:如何将私有存储库添加为依赖项?
- python - 使表单仅显示 SelectField 中的某个值
- xml - 如何在 XPath 中获取具有多个子条件的所有元素?
- powershell - 忘记串扣
- c# - 未调用 BackgroundWorker DoWork 函数