What is the purpose of __name__?

前端 未结 1 1029
挽巷
挽巷 2021-01-14 11:30

What does __name__ do? I have only seen it paired with __main__ and nothing else.

I know that the classic if __name__ == __main__:

相关标签:
1条回答
  • 2021-01-14 11:55

    __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
    
    0 讨论(0)
提交回复
热议问题