Skip to content

Instantly share code, notes, and snippets.

@diogobaltazar
Last active May 26, 2019 17:34
Show Gist options
  • Save diogobaltazar/ca946163a09ee8aafff379e4f530ae89 to your computer and use it in GitHub Desktop.
Save diogobaltazar/ca946163a09ee8aafff379e4f530ae89 to your computer and use it in GitHub Desktop.
modules
if __name__ == '__main__':
print('mod1 is executing by having been called directly')
else:
print('mod1 is executing by having been imported by module ' + __name__)
def f():
if __name__ == '__main__':
print('mod1.f is executing from module mod1')
else:
print('mod1.f is executing from module' + __name__)
f()
# > python mod1.py
# mod1 is executing by having been called directly
# mod1.f is executing from mod1
from mod1 import f
f()
# > python mod2.py
# mod1 is executing by having been imported by module mod1
# mod1.f is executing from module mod1
# mod1.f is executing from module mod1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment