栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python学习日记(6/8)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python学习日记(6/8)

入口文件和模块内置变量__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

作用:通常作为整个工程开始运行的入口

———完

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/958514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号