current position:Home>In Python__ name__ == ‘__ main__‘ Use of

In Python__ name__ == ‘__ main__‘ Use of

2022-02-02 14:53:29 Learnstening

When a module is first introduced by another program , Its main program will run . If we want to introduce modules , A block in a module does not execute , We can use __name__ Property to make the block execute only when the module itself is running .

if __name__ == '__main__':
   print(' The program itself is running ')
else:
   print(' I come from the first mock exam ')

The operation output is as follows :

$ python using_name.py
 The program itself is running 
$ python
>>> import using_name
 I come from the first mock exam 
>>>

explain :  Each module has one __name__ attribute , When the value is '__main__' when , Indicates that the module itself is running , Otherwise it's introduced .

explain :__name__  And  __main__  The bottom is double underline , _ _  That's how you get rid of the space in the middle .

copyright notice
author[Learnstening],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202021453277487.html

Random recommended