python 文档资源 dir __doc__

本文介绍了Python中如何使用dir()函数查看对象的所有属性,并解释了__doc__属性的作用及如何利用它为函数添加文档说明。同时展示了help()函数的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

形式:   角色

#注释 文档中的文档

dir 函数 对象中可用属性的列表

文档字符串:__doc__ 附加在对象上的文件中的文档

PyDoc:help函数 对象的交互帮助

PyDoc:HTML报表 浏览器中的模块文档

>>> import  sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', ...........omited']
>>> sys.__doc__
"This module provides access to some objects used or maintained by the\ninterpreter and to functions that interact strongly with the interpreter.\n\nDynamic objects:\n\nargv -- command line arguments; argv[0] is the script pathname if known\npath ,....................................omited"

使用dir查看内置对象类型提供了哪些属性,可以运行dir并且传入所需要类型的变量,例如查看列表的属性传入空对象即可

>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

__doc__使用

>>> def squre(data):
"""the squre is a function that.......
"""
return data**2


>>> squre.__doc__
'the squre is a function that.......\n\t'

help的使用

>>> help(sys.getrefcount)
Help on built-in function getrefcount in module sys:


getrefcount(...)
    getrefcount(object) -> integer
    
    Return the reference count of object.  The count returned is generally
    one higher than you might expect, because it includes the (temporary)
    reference as an argument to getrefcount().


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值