亲宝软件园·资讯

展开

python类方法注释规范

XerCis 人气:0

Python类和方法注释规范

这里写图片描述

这里写图片描述

注释风格

reStructuredTextPyCharm默认

def func(path, field_storage, temporary):
    '''基本描述
    详细描述
    :param path: The path of the file to wrap
    :type path: str
    :param field_storage: The :class:`FileStorage` instance to wrap
    :type field_storage: FileStorage
    :param temporary: Whether or not to delete the file when the File instance is destructed
    :type temporary: bool
    :returns: A buffered writable file descriptor
    :rtype: BufferedFileStorage
    '''
    pass

NumPy

def func(path, field_storage, temporary):
    '''基本描述
    详细描述
    Parameters
    ----------
    path : str
        The path of the file to wrap
    field_storage : FileStorage
        The :class:`FileStorage` instance to wrap
    temporary : bool
        Whether or not to delete the file when the File instance is destructed
    Returns
    -------
    BufferedFileStorage
        A buffered writable file descriptor
    '''
    pass

Google(官方推荐)

def func(path, field_storage, temporary):
    '''基本描述
    详细描述
    Args:
        path (str): The path of the file to wrap
        field_storage (FileStorage): The :class:`FileStorage` instance to wrap
        temporary (bool): Whether or not to delete the file when the File instance is destructed
    Returns:
        BufferedFileStorage: A buffered writable file descriptor
    '''
    pass
风格特点适用
reStructuredText用冒号分隔PyCharm默认
NumPy用下划线分隔倾向垂直,长而深的文档
Google用缩进分隔倾向水平,短而简单的文档

Sphinx对NumPy和Google风格的对比,英文不好可以参考中文版

小技巧

在PyCharm中Ctrl+Q可快速查看注释

代码规范(含代码注释)

代码缩进和冒号

注意条件语句必须严格控制缩进,保证父句和子句的关系

num = 10
if num>5:
    print('yes')
else:
    print('no')

空行分隔代码段

例如if语句判断、while循环、for循环、def函数、class类等代码段前后最好留一行(人工分好段落)

# if语句
if num>5:
    print('yes')
else:
    print('no')
 
# for循环
for i in (1,2,4):
    print(i)
 
# while循环
while i>3:
    print('yes')
    i+=1
else:
    print('end')
    
# 函数定义
def show():
    print(132)
 
# 类定义
class Person:
    def show(self):
        print(123)

包、模块的命名规范

1. 包——要求统一用小写(相当于文件夹)

2.模块——要求统一用小写(相当于文件夹里的文件)

类和对象的命名规范

1. 类——严格的驼峰式写法eg.IndexUserPerson

2. 对象——要求统一用小写

函数的命名规范

驼峰式写法 eg.indexUserPerson(不强行)

代码注释

1.单行注释——#

2.多行注释——(快捷键为Ctrl+/)

'''

三对单引号,python多行注释符''' 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。 

加载全部内容

相关教程
猜你喜欢
用户评论