0
# code of foo.py

var = 7
#code of foo1.py

import foo

print(foo.var) # it prints 7 

foo.var = 9  

print(foo.var) # it prints 9


#from "foo" reload(var)
import foo #reimporting module

print(foo.var) # still it prints 9 instead of 7 why?
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sumant W W
  • 11
  • 4

2 Answers2

1

The reason is that second import foo doesn't effect. See this question: What happens when a module is imported twice?.

hide1nbush
  • 759
  • 2
  • 14
  • Since the linked question doesn't have it, the python docs on this can be found here: [The Module Cache](https://docs.python.org/3/reference/import.html#the-module-cache) – SitiSchu Sep 15 '22 at 09:40
0

In python modules are singleton, meaning every import statement return the same instance of a module. As a consequence, any mutation of a module is visible to every scope that import this module.

https://docs.python.org/3/tutorial/modules.html#more-on-modules

WIP
  • 348
  • 2
  • 11