Uncategorized

scope – python global variable with multiple files how thay work?


how python’s global variable work?
if I declare them where are they in the memory?

z1.py

a = [123]

z2.py

from z1 import a

print(a)
print(a)

z3.py

from z1 import a

if __name__ == "__main__":
    print(a)
    a[0] = 234
    import z2

result

[123]
[234]
[234]

this time, work very well what I expected. but if I don’t use a list type the result different

z1.py

a = True

z2.py

from z1 import a

print(a)
print(a)

z3.py

from z1 import a

if __name__ == "__main__":
    print(a)
    a = False
    import z2

result

True
True
True

what’s the different? I think using list type maybe put they’re code in heap memory.
but when I use boolean also I think put they’re code in main stack area so work well.
but not.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *