gpt4 book ai didi

python - python 从模块导入哪些属性?

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:00 27 4
gpt4 key购买 nike

我检查了一些答案,例如 this ,但我还有另一个问题,即哪些属性是从 python 中的模块导入的。

例如,我有一个模块temp9.py:

a=10
b=20
print('a={0}, b={1}'.format(a,b))
def t9outer():

print('in imported module')
def t9inner():
print('inner function')

然后我像这样导入这个模块:import temp9。如果我使用此命令获取导入文件的属性:

list(filter(lambda x: not x.startswith('__'),dir(temp9)))

我得到这个输出:

['a', 'b', 't9outer']

我的问题是

  • ab 仅在 temp9 范围内是全局的,而不是跨模块的(如上面的答案所说) ,那么它是如何导出的呢?

  • 为什么 t9inner() 没有被导入,即使它包含在被导入的 t9outer() 中?

最佳答案

两个问题的答案是一样的。在模块级别定义的所有内容都被导入;任何未在模块级别定义的内容都不会导入。

关于python - python 从模块导入哪些属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46266640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com