繁体   English   中英

有没有办法从文件中导入所有函数(使用 *)而不导入该文件?

[英]Is there a way to import all functions(using *) from a file without the imports of that file?

我有两个.py文件

  1. Helper.py
  2. SomeClass.py

假设两个文件都导入 sys 的例子

我想像这样将Helper.py导入到SomeClass.py中: from helper.py import * (因为我需要其中的所有函数)

但我不想从任何文件中删除 import sys 因为这两个文件并不是真正的 go 手动。

有没有办法导入 * 并排除导入/从...导入另一个文件?

您可以定义__all__模块。

在您的Helper.py文件中,添加一行

__all__ = ["foo", "bar"]  # All the objects you want to export

然后在SomeClass.py中使用from helper.py import * ,这只会导入__all__中指定的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM