Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created October 3, 2017 17:17
Show Gist options
  • Save Audhil/a2358cf393956f0a24e9a8ac9932e6a9 to your computer and use it in GitHub Desktop.
Save Audhil/a2358cf393956f0a24e9a8ac9932e6a9 to your computer and use it in GitHub Desktop.
what is __name__ in python?
print ('top level in one.py')
def matter():
print ('this is matter() in one.py')
if __name__ == '__main__':
print("one.py is being run directly and __name__ is : ", __name__)
matter()
else:
print("one.py is being imported in another module and __name__ is : ", __name__)
$ python two.py
top level in one.py
('one.py is being imported in another module and __name__ is : ', 'one')
this is matter() in one.py
this is top level in two.py
('two.py is being run directly and __name__ is : ', '__main__')
import one
one.matter()
print ('this is top level in two.py')
if __name__ == '__main__':
print("two.py is being run directly and __name__ is : ", __name__)
else:
print("two.py is being imported in another module and __name__ is : ", __name__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment