Home  >  Article  >  Backend Development  >  How to quickly generate annotation documents in Python

How to quickly generate annotation documents in Python

高洛峰
高洛峰Original
2016-12-02 17:00:311327browse

How to quickly generate annotation documents with python

Today I will tell you a simple small detail that you only need to pay attention to, you can easily generate annotation documents, and you can also check whether the reference names of the class methods we wrote are repeated or have problems, etc. When you look at the documentation written by other professional experts, you will be envious. Don’t worry, we can let Python generate basically satisfactory documentation for us. Firstly, it can improve the overall readability of the code. Secondly, we can see the overall structure of the code. It is also clearer, which can save a lot of trouble during handover. When other colleagues take over your work, they will not ask you line by line what this is and what it is, because the comments have been expressed very intuitively, and in the integration Sometimes it can be used as a description document to give brief instructions to customers (mainly for your BOSS). Then let’s get to the point of how to implement it. See my simple code format below. Note that the comment place and function name and class name are all related to __all__.

#!/usr/bin/env python
# -*-coding:utf-8 -*-


'''
文档快速生成注释的方法介绍,首先我们要用到__all__属性
在Py中使用为导出__all__中的所有类、函数、变量成员等
在模块使用__all__属性可避免相互引用时命名冲突
'''
__all__ = ['Login', 'check', 'Shop', 'upDateIt', 'findIt', 'deleteIt', 'createIt']


class Login:
    '''
    测试注释一可以写上此类的作用说明等
    例如此方法用来写登录
    '''

    def __init__(self):
        '''
        初始化你要的参数说明
        那么登录可能要用到
        用户名username
        密码password
        '''
        pass

    def check(self):
        '''
        协商你要实现的功能说明
        功能也有很多例如验证
        判断语句,验证码之类的
        '''
        pass


class Shop:
    '''
    商品类所包含的属性及方法
    update改/更新
    find查找
    delete删除
    create添加
    '''

    def __init__(self):
        '''
        初始化商品的价格、日期、分类等
        '''
        pass

    def upDateIt(self):
        '''
        用来更新商品信息
        '''
        pass

    def findIt(self):
        '''
        查找商品信息
        '''
        pass

    def deleteIt(self):
        '''
        删除过期下架商品信息
        '''
        pass

    def createIt(self):
        '''
        创建新商品及上架信息
        '''
        pass

if __name__=="__main__":
    import pythonzhushi
    print help(pythonzhushi)

I would like to emphasize that the name of py must be the same as the name of the py you are currently working on (the name of the py I created here is pythonzhushi, and the final import is also this name), because in the following test, the import imports itself, so , the import name, Help (name) and your file name must be consistent. In order to be more intuitive, please look at the picture below:

How to quickly generate annotation documents in Python

Of course, you can also import from other files. Here we use the common import method of Python from which directory to import what files to import pythonzhushi

Of course I still use pictures here. Show the following:

How to quickly generate annotation documents in Python

Is it clear at a glance? In this way, we can also import the annotation documents we need to do. The help method is to help us view a simple help document for the classes and functions contained in this file, similar to the readme A description of the document. Finally, let’s take a look at the masking effect:

How to quickly generate annotation documents in Python

How to quickly generate annotation documents in Python

Is it possible to simply print out the description document of our program? In the following steps, you can directly create a text and paste this over to complete a readme document description. After testing A few points to note are that if you want to write a comment like #, don't write it inside the def. It should be written above it, similar to the decorator. On the contrary, don't write the three quotation marks ''' outside the def. This will have no display effect. . Everyone, hurry up and try it. thanks for watching. .

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn