It seems that if I import the same Python file via the import
statements from several files and change some file-scope variable in it, the reference change will not be visible to another modules while container changes will.
For example,
First example
first.py
import reader
import helper
reader.change_buf('modified')
helper.foo()
second.py
import reader
def foo():
print(reader.BUF)
reader.py
buf = 'original'
def change_buf(buf):
buf = buf
Output
> python first.py
original
Second example
first.py
import reader
import helper
reader.change_first_element('1')
helper.foo()
second.py
import reader
def foo():
print(reader.BUF)
reader.py
buf = ['0', '1', '2']
def change_first_element(new_elem):
buf[0] = new_elem
Output
> python first.py
['1', '1', '2']
Why?