python - Problem with variable scope in different files
某草草
某草草 2017-05-18 10:50:10
0
1
555

Now there are two files:

a.py:

x=1
from b import *
printx()
b.py:

def printx():
    print(x)

When calling $ python3 a.py from the command line, there will be a NameError. I don’t understand here. When calling a.py directly, isn’t x considered a global variable? According to the LEGB rules, why does it go wrong?

某草草
某草草

reply all(1)
phpcn_u1582

x is only visible in file a. Importing b in file a only makes the method printx in file b visible to file a. You can call it, and it does not change the invisibility of file x to file b. .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template