这样写python注释让代码更加的优雅


Posted in Python onJune 02, 2021

python这样注释,让你的代码看起来更加的优雅,是不是常常感觉自己的python代码写出来,看起来特别的乱,虽然可以正常运行,但是在优雅性上似乎欠缺的很多,这篇文章主要教你,如何让你的python代码看起来更加的优雅与美观,

一、注释欣赏

这里有一段飞兔小哥哥自己常写的注释模版

这里主要分为表头注释、类注释、欢迎语以及方法注释

表头注释会标注这个项目的名称、文件名、项目作者、时间等基础信息

类注释会标注这个类主要用来做什么的

而方法注释则表示当前方法的作用

​​#!/usr/bin/env python
# encoding: utf-8
'''
#-------------------------------------------------------------------
#                   CONFIDENTIAL --- CUSTOM STUDIOS
#-------------------------------------------------------------------
#
#                   @Project Name : the desc of project
#
#                   @File Name    : main.py
#
#                   @Programmer   : autofelix
#
#                   @Start Date   : 2021/06/01 12:42
#
#                   @Last Update  : 2021/06/01 12:42
#
#-------------------------------------------------------------------
'''
import requests, os, platform, time
from Crypto.Cipher import AES
import multiprocessing
from retrying import retry
 
class M3u8:
    '''
     This is a main Class, the file contains all documents.
     One document contains paragraphs that have several sentences
     It loads the original file and converts the original file to new content
     Then the new content will be saved by this class
    '''
    def __init__(self):
        '''
        Initial the custom file by self
        '''
        self.encrypt = False
 
    def hello(self):
        '''
        This is a welcome speech
        :return: self
        '''
        print("*" * 50)
        print(' ' * 15 + 'm3u8链接下载小助手')
        print(' ' * 5 + '作者: autofelix  Date: 2021-06-01 12:42')
        print(' ' * 10 + '适用于非加密 | 加密链接')
        print("*" * 50)
        return self
 
    def run(self):
        pass
 
if __name__ == '__main__':
    M3u8().hello().run()

附:python函数注释规范

首先来两段优秀开源框架的代码注释

例1 tornado.web.RequestHandler的get_arguments函数.

def get_argument(self, name, default=_ARG_DEFAULT, strip=True):
        """Returns the value of the argument with the given name.

        If default is not provided, the argument is considered to be
        required, and we raise a `MissingArgumentError` if it is missing.

        If the argument appears in the url more than once, we return the
        last value.

        The returned value is always unicode.
        """
        return self._get_argument(name, default, self.request.arguments, strip)

例2 requests的get函数

def get(url, params=None, **kwargs):
    """Sends a GET request.

    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """

    kwargs.setdefault('allow_redirects', True)
    return request('get', url, params=params, **kwargs)

对比下例1和例2, tornado框架的函数倾向与给出函数的用途说明,而不提供具体的输入参数说明,并且相对来说函数名字也是浅显易懂,而requests库看起来比较简洁一点,具体的输入和输出都给的很完整,看起来很是赏心悦目,所以我个人更偏向于例2的注释,当然,也有将例1和例2注释特点结合起来的库,比如tensorflow库,因为涉及的输入参数以及函数较为复杂,因此输入参数和函数原理有较为详尽的说明。总之,大部分编写函数的时候参考例2的注释方式,代码也看起来较为优雅,而遇到比较复杂的情况,则可以参考例1加上必要的函数详细说明。

总结

到此这篇关于python注释的文章就介绍到这了,更多相关python注释内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
对numpy的array和python中自带的list之间相互转化详解
Apr 13 Python
python实现自动网页截图并裁剪图片
Jul 30 Python
Python I/O与进程的详细讲解
Mar 08 Python
python操作小程序云数据库实现简单的增删改查功能
Jun 06 Python
Python实现微信小程序支付功能
Jul 25 Python
python爬虫 模拟登录人人网过程解析
Jul 31 Python
解决Django后台ManyToManyField显示成Object的问题
Aug 09 Python
Python类中方法getitem和getattr详解
Aug 30 Python
python创建学生管理系统
Nov 22 Python
python用WxPython库实现无边框窗体和透明窗体实现方法详解
Feb 21 Python
Django url 路由匹配过程详解
Jan 22 Python
OpenCV-Python实现油画效果的实例
Jun 08 Python
上帝为你开了一扇窗之Tkinter常用函数详解
只用20行Python代码实现屏幕录制功能
TensorFlow中tf.batch_matmul()的用法
Jun 02 #Python
pytorch 运行一段时间后出现GPU OOM的问题
Jun 02 #Python
python flask开发的简单基金查询工具
python爬取网页版QQ空间,生成各类图表
Python爬虫实战之爬取携程评论
You might like
解析php二分法查找数组是否包含某一元素
2013/05/23 PHP
php技术实现加载字体并保存成图片
2015/07/27 PHP
PHP简单判断手机设备的方法
2016/08/23 PHP
解决Yii2邮件发送结果返回成功,但接收不到邮件的问题
2017/05/23 PHP
Laravel5.5+ 使用API Resources快速输出自定义JSON方法详解
2020/04/06 PHP
PHPStorm2020.1永久激活及下载更新至2020(推荐)
2020/09/25 PHP
更换select下拉菜单背景样式的实现代码
2011/12/20 Javascript
js动态往表格的td中添加图片并注册事件
2014/06/12 Javascript
Jquery easyui开启行编辑模式增删改操作
2016/01/14 Javascript
基于js中的原型、继承的一些想法
2016/08/10 Javascript
angular.js之路由的选择方法
2016/09/24 Javascript
适用于手机端的jQuery图片滑块动画
2016/12/09 Javascript
Input文本框随着输入内容多少自动延伸的实现
2017/02/15 Javascript
提高JavaScript执行效率的23个实用技巧
2017/03/01 Javascript
Angular.js中下拉框实现渲染html的方法
2017/06/18 Javascript
H5基于iScroll实现下拉刷新和上拉加载更多
2017/07/18 Javascript
Nodejs中使用phantom将html转为pdf或图片格式的方法
2017/09/18 NodeJs
JavaScript定义及输出螺旋矩阵的方法详解
2017/12/01 Javascript
Vue SSR 组件加载问题
2018/05/02 Javascript
vue进入页面时滚动条始终在底部代码实例
2019/03/26 Javascript
react-native 实现购物车滑动删除效果的示例代码
2021/01/15 Javascript
Python中Django框架下的staticfiles使用简介
2015/05/30 Python
在python 中split()使用多符号分割的例子
2019/07/15 Python
Django models.py应用实现过程详解
2019/07/29 Python
python matplotlib画盒图、子图解决坐标轴标签重叠的问题
2020/01/19 Python
匈牙利超级网上商店和优惠:Alza.hu
2019/12/17 全球购物
数据库面试要点基本概念
2013/10/31 面试题
给导游的表扬信
2014/01/10 职场文书
小区门卫的岗位职责
2014/09/26 职场文书
公司周年庆典标语
2014/10/07 职场文书
国庆节标语大全
2014/10/08 职场文书
人事局接收函
2015/01/31 职场文书
2019年度行政文员工作计划范本!
2019/07/04 职场文书
MySQL infobright的安装步骤
2021/04/07 MySQL
JVM的类加载器和双亲委派模式你了解吗
2022/03/13 Java/Android
Python利用Turtle绘制哆啦A梦和小猪佩奇
2022/04/04 Python