Python 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基础12-内置函数
- Python 进制转换函数及前缀截取
- python电脑版微信-微信PC版的API接口 | 可通过Python调用微信功能
- python进制转换函数-Python中进制转换函数的使用
- python之open函数
- python的random()函数用法_Python随机函数random用法示例
- python中bool函数_bool()函数以及Python中的示例
- python爬虫—–Python访问http的几种方式「建议收藏」
- 【说站】python析构函数的底层机制
- 【说站】python ReLU函数是什么
- 【说站】python中camel函数的使用
- h5 Python_python做h5网站
- python deepcopy函数_Python deepcopy
- 神经网络 | 感知器原理及python代码实现and和or函数
- python TCP服务器v1.2 - 服务端新增用户登录注册(json, md5加密)
- Python面向对象编程-三大特性-继承
- python raise抛出异常
- python-Python与SQLite数据库-SQLite数据库的基本知识(一)
- 一组python用于验证数据的函数详解编程语言
- python 定时执行指定的函数详解编程语言
- Linux中运行Python文件的步骤(linux运行python文件)
- Python中用format函数格式化字符串的用法
- Python编程连接MySQL:从零开始(python与mysql)
- 手把手教你用 Python 实现针对时间序列预测的特征选择
- python合并文件的具体实例
- python基础教程之popen函数操作其它程序的输入和输出示例
- 零基础写python爬虫之爬虫框架Scrapy安装配置
- Python中apply函数的用法实例教程
- python中的函数用法入门教程