这样写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 相关文章推荐
Python3的urllib.parse常用函数小结(urlencode,quote,quote_plus,unquote,unquote_plus等)
Sep 18 Python
Python正则表达式知识汇总
Sep 22 Python
python读取图片并修改格式与大小的方法
Jul 24 Python
python爬虫获取百度首页内容教学
Dec 23 Python
Python2与Python3的区别点整理
Dec 12 Python
解决pycharm每次打开项目都需要配置解释器和安装库问题
Feb 26 Python
利用Python制作动态排名图的实现代码
Apr 09 Python
使用Python和百度语音识别生成视频字幕的实现
Apr 09 Python
python IDLE添加行号显示教程
Apr 25 Python
使用matplotlib的pyplot模块绘图的实现示例
Jul 12 Python
Python求区间正整数内所有素数之和的方法实例
Oct 13 Python
python实现数学模型(插值、拟合和微分方程)
Nov 13 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中用date函数获取当前时间有误的解决办法
2013/08/02 PHP
合并ThinkPHP配置文件以消除代码冗余的实现方法
2014/07/22 PHP
php实现的简易扫雷游戏实例
2015/07/09 PHP
php生成Android客户端扫描可登录的二维码
2016/05/13 PHP
PHP怎样用正则抓取页面中的网址
2016/08/09 PHP
laravel excel 上传文件保存到本地服务器功能
2019/11/14 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
2020/02/16 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
php gethostbyname获取域名ip地址函数详解
2010/01/24 Javascript
改变javascript函数内部this指针指向的三种方法
2010/04/23 Javascript
juqery 学习之四 筛选过滤
2010/11/30 Javascript
自动设置iframe大小的jQuery代码
2013/09/11 Javascript
一个JavaScript用逗号分割字符串实例
2014/09/22 Javascript
使用C++为node.js写扩展模块
2015/04/22 Javascript
jQuery animate easing使用方法图文详解
2016/06/17 Javascript
js字符串操作总结(必看篇)
2016/11/22 Javascript
使用JavaScript生成罗马字符的实例代码
2018/06/08 Javascript
Vue filter格式化时间戳时间成标准日期格式的方法
2018/09/16 Javascript
vue发送ajax请求详解
2018/10/09 Javascript
JS实现压缩上传图片base64长度功能
2019/12/03 Javascript
JS控制下拉列表左右选择实例代码
2020/05/08 Javascript
JavaScript实现切换多张图片
2021/01/27 Javascript
python中的装饰器详解
2015/04/13 Python
python+selenium select下拉选择框定位处理方法
2019/08/24 Python
python如何通过闭包实现计算器的功能
2020/02/22 Python
美国受欢迎的眼影品牌:BH Cosmetics
2016/10/25 全球购物
红色连衣裙精品店:Red Dress Boutique
2018/08/11 全球购物
MyHeritage美国:家族史研究和DNA测试的领先服务
2019/05/27 全球购物
九年级政治教学反思
2014/02/06 职场文书
2014年党员学习“三严三实”思想汇报
2014/09/15 职场文书
上课说话检讨书
2015/01/27 职场文书
汤姆索亚历险记读书笔记
2015/06/29 职场文书
幼儿园毕业典礼园长致辞
2015/07/29 职场文书
2015年国培研修感言
2015/08/01 职场文书
母亲节主题班会
2015/08/14 职场文书
漫画「狩龙人拉格纳」公开TV动画预告图
2022/03/22 日漫