dir用于列出python对象所有的方法,如下以字符串类str为例:
dir (str)
#结果如下:
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__',
'__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace',
'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
然后可以通过help来查询自己需要使用的方法的具体描述:
help(str.split)
#输出结果如下:
Help on method_descriptor:
split(...)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.
help其他功能补充:
- 查看python所有的modules:help(“modules”)
- 单看python所有的modules中包含指定字符串的modules: help(“modules yourstr”)
- 查看python中常见的topics: help(“topics”)
- 查看python标准库中的module:import os.path + help(“os.path”)
- 查看python内置的类型:help(“list”)
- 查看python类型的成员方法:help(“str.find”)
- 查看python内置函数:help(“open”)