简体   繁体   中英

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.

For example, I have a module 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 . 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?

  • Why was t9inner() not imported even though it is enclosed by t9outer() , which gets imported?

The answer is the same for both questions. Everything defined at module level is imported; anything not defined at module level is not imported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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