0

A.py:

X = 10

B.py:

import A
A.X = 100

C.py:

import A
Print("A.X = ",A.X)

If I execute B and then C , I get A.X = 10. But what about the changes made by module B on X? Why the changes are not reflecting in module C?

2
  • "If I execute B and then C" Do you execute them in the same process or in one? How do you do so? Commented May 2, 2019 at 11:26
  • This may be relevant: stackoverflow.com/questions/55475928/… Commented May 2, 2019 at 11:27

1 Answer 1

1

Python scripts run in separate shells with separate process, memories, namespaces, etc. Modifying a module attribute in a script is a in-memory operation: it does not affect the file that the module was loaded from:

 $ python B.py

This will create a python process, load B.py, then A.py, and modify the dictionary corresponding to the namespace of module A. Then the process will end, losing anything you didn't write to disk.

$ python C.py

This will create a python process, load C.py, then A.py, and print the freshly loaded value from the dictionary of the newly read-in module A. This will be whatever was in the file A.py.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.