I am new to python and started with some of the basic example and found a question that is does an imported python module will have a direct access to the globals in the module which imports it or vice versa.
Below is my code:
x.py
import y
def f():
global x
x = 6
print(x)
def main():
global x
x = 3
print(x)
f()
y.g()
if __name__ == '__main__':
main()
y.py
def g():
global x
x += 1
print(x)
Below is the traceback:
3
6
Traceback (most recent call last):
File "C:\Users\abc\Desktop\x.py", line 16, in <module>
main()
File "C:\Users\abc\Desktop\x.py", line 13, in main
y.g()
File "C:\Users\abc\Desktop\y.py", line 3, in g
x += 1
NameError: name 'x' is not defined