Reputation: 67
#conf.py
def init():
global mylist
mylist=[]
#change.py
import conf
def change():
if __name__ == "__main__":
print('Direct')
conf.mylist.append('Directly executed')
print(conf.mylist)
else:
conf.mylist.append('It was imported')
#exec.py
import conf
import change
conf.init()
change.change()
print (conf.mylist)
When running exec.py the result is what I expected, but when running change.py directly I didn't get any output (no Direct , no conf.mylist
)
Upvotes: 1
Views: 316
Reputation: 44830
Yes, that's the normal behavior. You need to call the change
function for this code to execute.
You can add the following to the end of change.py
if __name__=="__main__":
change()
Upvotes: 9
Reputation: 82440
This is because change
is never invoked. Invoke it at the end of the file with change()
Upvotes: 2