简体   繁体   English

python从模块导入什么属性?

[英]What attributes does python import from a module?

I checked some answers like this , but I have another question about which attributes get imported from a module in python. 我检查了像这样的一些答案,但是还有另一个问题,关于哪些属性是从python模块中导入的。

For example, I have a module temp9.py : 例如,我有一个模块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')

Then I import this module like so: import temp9 . 然后像这样导入该模块: import temp9 If I get the attributes of the imported file using this command: 如果使用以下命令获取导入文件的属性:

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

I get this output: 我得到以下输出:

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

My questions are 我的问题是

  • a and b are global only in the scope of temp9 , not across the modules (as the above answer says), so how does it get exported? ab仅在temp9的范围内是temp9 ,而不是在模块之间(如上述答案所述)是全局的,那么如何导出它?

  • Why was t9inner() not imported even though it is enclosed by t9outer() , which gets imported? 为什么t9inner()不是,即使它是由封闭进口t9outer()它获取进口?

The answer is the same for both questions. 两个问题的答案都是相同的。 Everything defined at module level is imported; 在模块级别定义的所有内容都将被导入; anything not defined at module level is not imported. 任何未在模块级别定义的内容都不会导入。

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

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