百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

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

zhezhongyun 2024-12-04 17:02 104 浏览

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文件内容:

1.6 python手册

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

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

相关推荐

perl基础——循环控制_principle循环

在编程中,我们往往需要进行不同情况的判断,选择,重复操作。这些时候我们需要对简单语句来添加循环控制变量或者命令。if/unless我们需要在满足特定条件下再执行的语句,可以通过if/unle...

CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅

CHAPTER1TheThreePresentsofD'ArtagnantheElderCHAPTER2TheAntechamber...

CHAPTER 5 The King'S Musketeers and the Cardinal'S Guards 第五章 国王的火枪手和红衣主教的卫士

CHAPTER3TheAudienceCHAPTER5TheKing'SMusketeersandtheCardinal'SGuard...

CHAPTER 3 The Audience 第三章 接见

CHAPTER3TheAudienceCHAPTER3TheAudience第三章接见M.DeTrévillewasatt...

别搞印象流!数据说明谁才是外线防守第一人!

来源:Reddit译者:@assholeeric编辑:伯伦WhoarethebestperimeterdefendersintheNBA?Here'sagraphofStea...

V-Day commemorations prove anti-China claims hollow

People'sLiberationArmyhonorguardstakepartinthemilitaryparademarkingthe80thanniversary...

EasyPoi使用_easypoi api

EasyPoi的主要特点:1.设计精巧,使用简单2.接口丰富,扩展简单3.默认值多,writelessdomore4.springmvc支持,web导出可以简单明了使用1.easypoi...

关于Oracle数据库12c 新特性总结_oracle数据库12514

概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...

【开发者成长】JAVA 线上故障排查完整套路!

线上故障主要会包括CPU、磁盘、内存以及网络问题,而大多数故障可能会包含不止一个层面的问题,所以进行排查时候尽量四个方面依次排查一遍。同时例如jstack、jmap等工具也是不囿于一个方面的问题...

使用 Python 向多个地址发送电子邮件

在本文中,我们将演示如何使用Python编程语言向使用不同电子邮件地址的不同收件人发送电子邮件。具体来说,我们将向许多不同的人发送电子邮件。使用Python向多个地址发送电子邮件Python...

提高工作效率的--Linux常用命令,能够决解95%以上的问题

点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf为什么要学习Linux命令?1、因为Li...

linux常用系统命令_linux操作系统常用命令

系统信息arch显示机器的处理器架构dmidecode-q显示硬件系统部件-(SMBIOS/DMI)hdparm-i/dev/hda罗列一个磁盘的架构特性hdparm-tT/dev/s...

小白入门必知必会-PostgreSQL-15.2源码编译安装

一PostgreSQL编译安装1.1下载源码包在PostgreSQL官方主页https://www.postgresql.org/ftp/source/下载区选择所需格式的源码包下载。cd/we...

Linux操作系统之常用命令_linux系统常用命令详解

Linux操作系统一、常用命令1.系统(1)系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本dmidecode-q显示硬件系...

linux网络命名空间简介_linux 网络相关命令

此篇会以例子的方式介绍下linux网络命名空间。此例中会创建两个networknamespace:nsa、nsb,一个网桥bridge0,nsa、nsb中添加网络设备veth,网络设备间...