-2

I have gone through various post and explanations for the above keywords [__name__ and __main__].

Please confirm if my understanding is Correct

1.__name__ saves the name of the file automatically when a file is edited & saved.

2.__main__ is updated through command prompt.

What is the value stored in __main__ when its not initialized ?

Can I program the __main__ to access the conditional code in the file?

For example, I have two files a.py and b.py

a.py

import b
# Can I program here '__main__' to access the functionality in b?
# So that I can print the condition is true if __name__ == '__main__':

b.py

if __name__ == '__main__':
    print 'This program is being run by itself'    
else:
    print 'I am being imported from another module'

Please comment your views on understanding and Possibility of access/modifying __main__

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
user2598064
  • 157
  • 1
  • 13

1 Answers1

2

__name__ will give you the name of the current module and module which is used when you say

python prog.py

__name__ in prog.py would now be __main__. This has been explained in python docs here

thefourtheye
  • 233,700
  • 52
  • 457
  • 497