入口文件和模块内置变量__name__不同
一、__name__的经典应用d
1、dir()函数
程序员都不能全部即住所有的⽅法,这时候 dir() ⽅法就⾮常有⽤了,使⽤ dir()函
数可以查看对象内的所有的属性和⽅法,在 python 中任何东西都是对象,⼀种数据类型,⼀个模块等,都有⼦集的属性和⽅法,除了常⽤
的⽅法外,其他的你不需要全部记住它,交给 dir() 函数就好了。
--------------------------------------------------------
作者:无冬水云99
链接:https://wenku.baidu.com/view/ef9b4d3117791711cc7931b765ce0508763275e0.html
来源:百度文库
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
例:
import sys
infos = dir(sys)
print(infos)
结果:
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace',
'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
2、__name__的用法
例:在demo9中:
if __name__ == '__main__':
print('this is an app ')
print('this is a medule')
#使脚本即可被调用也可执行
结果:
this is an app
this is a medule
在demo10中:
import demo9
结果:
this is a medule
结论:
if __name__ == '__main__':
pass
作用:通常作为整个工程开始运行的入口
———完