Or the use and interpretation of __name__ basis.

__name__ can return a class or a function name.

def test():
    pass

class Human:
    pass

print(test.__name__)
print(Human.__name__)
test
Human

 We write in the program, it often used __name__ == __main__

Then run directly because __name__ directly in the module or can you write code.

python inside a shell of any direct execution __name__ must return "__main__", because in the main function, the name of the module is run __main__.

But if, as is called by other modules, it is in the implementation __name__ on it a file name.

Here I make a comparison of different tests.

Print ( __name__ ) 


IF  __name__ == ' a1 ' :    # Because __name__ a1 is not equal to the back is not performed so 
    Print (1111)
__main__

But when I:

import a1

 Export

a1 
1111

 Because a1 is introduced as a module, its name has become a1, behind the print output can be properly executed.

Guess you like

Origin www.cnblogs.com/sidianok/p/11922367.html