Python中dir()和help()函数如何使用?
- 内容介绍
- 文章标签
- 相关推荐
本文共计803个文字,预计阅读时间需要4分钟。
我们学习了许多字符串处理方法,包括`split()`, `join()`, `find()`, `index()`等,但这些都是常用方法,并非全部。由于篇幅限制,本章仅介绍一些最常见的字符串操作方法,其他方法将留待后续章节。
前面我们已经学习了很多字符串提供的方法,包括 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']
__开头和结尾的方法都是私有的,不能在类的外部调用。使用 help() 查看 str 类型中 lower() 函数的用法:
>>> help(str.lower)
Help on method_descriptor:
lower(self, /)
Return a copy of the string converted to lowercase.
注意,使用 help() 查看某个函数的用法时,函数名后边不能带括号,例如将上面的命令写作
help(str.lower())就是错误的。本文共计803个文字,预计阅读时间需要4分钟。
我们学习了许多字符串处理方法,包括`split()`, `join()`, `find()`, `index()`等,但这些都是常用方法,并非全部。由于篇幅限制,本章仅介绍一些最常见的字符串操作方法,其他方法将留待后续章节。
前面我们已经学习了很多字符串提供的方法,包括 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']
__开头和结尾的方法都是私有的,不能在类的外部调用。使用 help() 查看 str 类型中 lower() 函数的用法:
>>> help(str.lower)
Help on method_descriptor:
lower(self, /)
Return a copy of the string converted to lowercase.
注意,使用 help() 查看某个函数的用法时,函数名后边不能带括号,例如将上面的命令写作
help(str.lower())就是错误的。
