专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

Python dir()和help()帮助函数

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/17 15:28:54       共计:3626 浏览
前面我们已经学习了很多字符串提供的方法,包括 split()、join()、find()、index() 等,但这远远不是它的全部方法。由于篇幅有限,本章只能给大家列举一些最常用的方法,至于其他的方法,读者可通过本节介绍的 dir() 和 help() 函数自行查看。

Python dir() 函数用来列出某个类或者某个模块中的全部内容,包括变量、方法、函数和类等,它的用法为:

dir(obj)

obj 表示要查看的对象。obj 可以不写,此时 dir() 会列出当前范围内的变量、方法和定义的类型。

Python help() 函数用来查看某个函数或者模块的帮助文档,它的用法为:

help(obj)

obj 表示要查看的对象。obj 可以不写,此时 help() 会进入帮助子程序。

掌握了以上两个函数,我们就可以自行查阅 Python 中所有方法、函数、变量、类的用法和功能了。

【实例】使用 dir() 查看字符串类型(str)支持的所有方法:

>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

在 Python 标准库中,以__开头和结尾的方法都是私有的,不能在类的外部调用。

【实例】使用 help() 查看 str 类型中 lower() 函数的用法:

>>> help(str.lower)
Help on method_descriptor:

lower(self, /)
    Return a copy of the string converted to lowercase.

可以看到,lower() 函数用来将字符串中的字母转换为小写形式,并返回一个新的字符串。

注意,使用 help() 查看某个函数的用法时,函数名后边不能带括号,例如将上面的命令写作help(str.lower())就是错误的。
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:Python split()方法详解:分割字符串 | ·下一条:Spring AOP(面向切面编程)是什么?

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有