Reputation: 5651
What does __name__
do? I have only seen it paired with __main__
and nothing else.
I know that the classic if __name__ == __main__:
defines the behavior when acting as a package vs running as stand-alone.
However what other usages are there for __name__
?
Upvotes: 4
Views: 2226
Reputation: 1059
__name__
is "__main__"
if you're executing the script directly. If you're importing a module, __name__
is the name of the module.
foo.py:
print(__name__)
bar.py
import foo
Run the scripts:
$ python foo.py
__main__
$ python bar.py
foo
Upvotes: 11