I have two scripts, test1.py
and test2.py
, with the following contents:
# test1.py
box = []
def run():
box.append(20)
box.append(30)
print(box)
print(f'test1 {id(box)}')
from test2 import b
print(b)
if __name__ == '__main__':
run()
# test2.py
from test1 import box
print(f'test2 {id(box)}')
box.append(40)
b = box
And this is the result I get when I run test1.py
:
[20, 30]
test1 4320983560
test2 4320994504
[40]
In my opinion box
is a mutable object, so the final result should be [20, 30, 40]
, but in fact, box
in test1.py
and test2.py
has a different id and it looks not the same one. This confuses me, can anybody tell me why?