Which object is the property of _ _ name__ in python? Is it equivalent to self.__name__?

print(__name__,type(__name__))

result: _ _ main__ < class" str" >
my question is, which object"s attribute is _ _ name__? Is it equivalent to self.__name__? Or thismodule.__name__?

after the wind blows, it is sure enough

import pandas as pd
print(pd.__name__)

pandas

May.09,2022

the name of the current main program


_ _ name__ is a built-in variable whose value is obtained by the name of the evaluation module / package. So if you import a module / package, it is naturally the name of the module / package. But if you run a python file directly, _ _ name__ is _ _ main__ when running as a top-level script. So you will often see

if __name__ == '__main__':
 code_block
As a rule, if you are designing a functional module, in other words, if you want to be imported by the user, then you should implement a simple unit test or sanity check in the code_block above. Because this part of the code will not trigger during import. It is triggered only when it is run directly.

after the wind blew, the god hinted and did the test again, and sure enough:

import pandas as pd
print(pd.__name__)
< hr >

pandas

Menu