python文档资源及pydoc生成html文件

python文档资源包括下面几种:

序号 形式 描述
1 #注释 程序语句对应的注释
2 dir() 查看对象全部属性
3 doc 文档字符串
4 help() 查看对象具体属性用法
5 HTML报表 html格式帮助文档
6 标准手册 python语言和库的说明
7 网站资源 在线教程、技术博客
8 书籍资源 相关书籍

1.1 #注释

python井号(#)用于程序语句对应的注释。

#号后面直到行末的内容都会当做注释,不被执行。

python通过#注释的内容只能在程序原文件查看。

1.2 dir()

python的dir(对象)内置函数,返回对象全部属性组成的列表。

示例

# 通过常量表达式生成实例后查看
>>> dir('')
['__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']
# 通过类型名生成实例后查看
>>> 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']
>>> dir('')==dir(str)

1.3 doc

python文档字符串__doc__,值为模块文件开头、函数开头、类开头、方法开头的注释,python会自动封装这些注释,并且保存在__doc__。这些注释写在三引号内。

1.3.1 自定义文档字符串

文档字符串可以通过不同位置路径对象的__doc__获取。

不同路径对象属性名(函数名、类名、方法名)可以通过dir(模块)获取。

模块:模块名.doc

函数:模块名.函数名.doc

类:模块名.类名.doc

方法名:模块名.类名.方法名.doc

示例

'''
模块文件名:docstr.py
模块开头的文档字符串
'''
S='梯阅线条'
def hellof(name):
    '''
    函数开头的文档字符串
    '''
    print('hello ',name)

class Student:
    '''
    类开头处的文档字符串
    '''
    def study(self):
        '''
        方法开头的文档字符串
        '''
        pass
    
# 查看不同对象的__doc__文档字符串
>>> path=r'E:\documents\F盘'
>>> import os
>>> os.chdir(path)
>>> import docstr
>>> dir(docstr)
['L', 'S', 'Student', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'hellof']
>>> print(docstr.__doc__)

模块文件名:docstr.py
模块开头的文档字符串
>>> print(docstr.hellof.__doc__)

    函数开头的文档字符串
>>> print(docstr.Student.__doc__)

    类开头处的文档字符串
>>> print(docstr.Student.study.__doc__)

        方法开头的文档字符串
        

1.4 help()

python的help()内置函数查看传入对象的使用说明,传入对象可以是模块名、函数名、类名、方法名、变量引用。

示例

>>> path=r'E:\documents\F盘'
>>> import os
>>> os.chdir(path)
>>> import docstr
>>> help(docstr)
Help on module docstr:

NAME
    docstr

DESCRIPTION
    模块文件名:docstr.py
    模块开头的文档字符串

CLASSES
    builtins.object
        Student
    
    class Student(builtins.object)
     |  类开头处的文档字符串
     |  
     |  Methods defined here:
     |  
     |  study(self)
     |      方法开头的文档字符串
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)

FUNCTIONS
    hellof(name)
        函数开头的文档字符串

DATA
    L = ['梯', '阅', '线', '条']
    S = '梯阅线条'

FILE
    e:\documents\f盘\docstr.py


>>> help(docstr.hellof)
Help on function hellof in module docstr:

hellof(name)
    函数开头的文档字符串

1.5 pydoc

1.5.1 生成html

用法

python -m pydoc -w docstr

描述

进入到docstr.py文件的目录,执行用法里面的语句。

执行pydoc模块,将docstr的文档字符串写入到docstr.html文件,成为帮助文档。

-m:表示运行模块(module),后面接模块名

-w:后接要生成html文档的模块名,表示将模块的文档字符串写入到模块名.html文件中。

示例

E:\documents\F盘>python -m pydoc -w docstr
wrote docstr.html

会生成类似下面的html文件内容:

python文档资源及pydoc生成html文件_第1张图片

1.6 python手册

python安装目录的doc目录下python378.chm。

更多内容参考python知识分享或软件测试开发目录。

你可能感兴趣的:(python,python,python学习资料)