这样写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 相关文章推荐
python中__slots__用法实例
Jun 04 Python
浅谈Python中列表生成式和生成器的区别
Aug 03 Python
python脚本实现xls(xlsx)转成csv
Apr 10 Python
Python中列表元素转为数字的方法分析
Jun 14 Python
rabbitmq(中间消息代理)在python中的使用详解
Dec 14 Python
分析python动态规划的递归、非递归实现
Mar 04 Python
Django 限制用户访问频率的中间件的实现
Aug 23 Python
python多进程读图提取特征存npy
May 21 Python
python logging模块书写日志以及日志分割详解
Jul 22 Python
Django自定义用户表+自定义admin后台中的字段实例
Nov 18 Python
Python使用正则表达式实现爬虫数据抽取
Aug 17 Python
Python答题卡识别并给出分数的实现代码
Jun 22 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使用fputcsv()函数csv文件读写数据的方法
2015/01/06 PHP
抛弃 PHP 代价太高
2016/04/26 PHP
对于Laravel 5.5核心架构的深入理解
2018/02/22 PHP
PHP面向对象程序设计之对象的遍历操作示例
2019/06/12 PHP
js 利用className得到对象的实现代码
2011/11/15 Javascript
子窗体与父窗体传值示例js代码
2013/08/01 Javascript
jquery.cookie用法详细解析
2013/12/18 Javascript
使用JavaScript 编写简单计算器
2014/11/24 Javascript
简单实现限制uploadify上传个数
2015/11/16 Javascript
MVC Ajax Helper或Jquery异步加载部分视图
2015/11/29 Javascript
bootstrap3 兼容IE8浏览器!
2016/05/02 Javascript
jQuery Easyui Datagrid实现单行的上移下移及保存移动的结果
2016/08/15 Javascript
js实现类bootstrap模态框动画
2017/02/07 Javascript
12个非常有用的JavaScript技巧
2017/05/17 Javascript
原生js实现仿window10系统日历效果的实例
2017/10/31 Javascript
Vue入门之animate过渡动画效果
2018/04/08 Javascript
Vue不能观察到数组length的变化
2018/06/08 Javascript
vue 使用自定义指令实现表单校验的方法
2018/08/28 Javascript
解决vue-cli webpack打包后加载资源的路径问题
2018/09/25 Javascript
vue+express+jwt持久化登录的方法
2019/06/14 Javascript
JS为什么说async/await是generator的语法糖详解
2019/07/11 Javascript
[01:19:11]Ti4 循环赛第二日 NaVi.us vs iG
2014/07/11 DOTA
python字典序问题实例
2014/09/26 Python
Python中使用异常处理来判断运行的操作系统平台方法
2015/01/22 Python
pandas读取csv文件,分隔符参数sep的实例
2018/12/12 Python
Python&amp;&amp;GDAL实现NDVI的计算方式
2020/01/09 Python
Python 如何反方向迭代一个序列
2020/07/28 Python
HomeAway澳大利亚:预订你的度假屋,公寓、度假村、别墅等
2019/02/20 全球购物
班组长工作职责
2013/12/25 职场文书
计算机大学生职业生涯规划书范文
2014/02/19 职场文书
就业意向书范文
2014/04/01 职场文书
大队干部竞选演讲稿
2014/04/28 职场文书
体育专业自荐书
2014/05/29 职场文书
ajax请求前端跨域问题原因及解决方案
2021/10/16 Javascript
html5+实现plus.io进行拍照和图片等获取
2022/06/01 HTML / CSS
MySQL深分页问题解决思路
2022/12/24 MySQL