这样写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实现排序算法
Feb 14 Python
python求列表交集的方法汇总
Nov 10 Python
python爬虫headers设置后无效的解决方法
Oct 21 Python
对Python2与Python3中__bool__方法的差异详解
Nov 01 Python
Python解析、提取url关键字的实例详解
Dec 17 Python
centos7中安装python3.6.4的教程
Dec 11 Python
Python 操作 PostgreSQL 数据库示例【连接、增删改查等】
Apr 21 Python
python 轮询执行某函数的2种方式
May 03 Python
Django多层嵌套ManyToMany字段ORM操作详解
May 19 Python
Python常用模块函数代码汇总解析
Aug 31 Python
Python可视化工具如何实现动态图表
Oct 23 Python
Python爬虫新手入门之初学lxml库
Dec 20 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
用C/C++扩展你的PHP 为你的php增加功能
2012/09/06 PHP
php制作中间带自己定义图片二维码的方法
2014/01/27 PHP
php读取csv数据保存到数组的方法
2015/01/03 PHP
php中实现用数组妩媚地生成要执行的sql语句
2015/07/10 PHP
thinkPHP实现递归循环栏目并按照树形结构无限极输出的方法
2016/05/19 PHP
遍历echsop的region表形成缓存的程序实例代码
2016/11/01 PHP
php实例化一个类的具体方法
2019/09/19 PHP
JObj预览一个JS的框架
2008/03/13 Javascript
JavaScript 错误处理与调试经验总结
2010/08/10 Javascript
file模式访问网页时iframe高度自适应解决方案
2013/01/16 Javascript
jQuery自动切换/点击切换选项卡效果的小例子
2013/08/12 Javascript
jQuery实现从身份证号中获取出生日期和性别的方法分析
2016/02/25 Javascript
js通过指定下标或指定元素进行删除数组的实例
2017/01/12 Javascript
JS实现求数组起始项到终止项之和的方法【基于数组扩展函数】
2017/06/13 Javascript
Vue2.0 实现单选互斥的方法
2018/04/13 Javascript
angular实现input输入监听的示例
2018/08/31 Javascript
vue2.0 使用element-ui里的upload组件实现图片预览效果方法
2018/09/04 Javascript
Nodejs核心模块之net和http的使用详解
2019/04/02 NodeJs
Bootstrap 时间日历插件bootstrap-datetimepicker配置与应用小结
2019/05/28 Javascript
如何使用JS console.log()技巧提高工作效率
2020/10/14 Javascript
[38:30]2014 DOTA2国际邀请赛中国区预选赛 LGD-GAMING VS CIS 第一场2
2014/05/24 DOTA
利用Anaconda完美解决Python 2与python 3的共存问题
2017/05/25 Python
Python 通过打码平台实现验证码的实现
2019/05/13 Python
利用Python的turtle库绘制玫瑰教程
2019/11/23 Python
python3 pathlib库Path类方法总结
2019/12/26 Python
Python flask路由间传递变量实例详解
2020/06/03 Python
使用CSS3在触屏上为按钮实现激活效果
2013/09/27 HTML / CSS
html5响应式开发自动计算fontSize的方法
2020/01/13 HTML / CSS
kmart凯马特官网:美国最大的打折零售商和全球最大的批发商之一
2016/11/17 全球购物
如何为DataGridView添加一个定制的Column Type
2014/01/21 面试题
标准化管理实施方案
2014/02/25 职场文书
英文请假条
2014/04/11 职场文书
降价通知函
2015/04/23 职场文书
欠款证明
2015/06/24 职场文书
纪律委员竞选稿
2015/11/19 职场文书
Oracle数据库中通用的函数实例详解
2022/03/25 Oracle