这样写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中使用wxPython开发的一个简易笔记本程序实例
Feb 08 Python
Python实现国外赌场热门游戏Craps(双骰子)
Mar 31 Python
Python实现在线暴力破解邮箱账号密码功能示例【测试可用】
Sep 06 Python
关于Python作用域自学总结
Jun 10 Python
Python制作微信好友背景墙教程(附完整代码)
Jul 17 Python
Django CBV与FBV原理及实例详解
Aug 12 Python
解析PyCharm Python运行权限问题
Jan 08 Python
python GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的详细方法与实例
Mar 09 Python
调整Jupyter notebook的启动目录操作
Apr 10 Python
Linux安装Python3如何和系统自带的Python2并存
Jul 23 Python
利用python实现汉诺塔游戏
Mar 01 Python
Python机器学习之底层实现KNN
Jun 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
php判断输入不超过mysql的varchar字段的长度范围
2011/06/24 PHP
php中最简单的字符串匹配算法
2014/12/16 PHP
php foreach如何跳出两层循环(详解)
2016/11/05 PHP
基于Laravel 多个中间件的执行顺序详解
2019/10/21 PHP
突发奇想的一个jquery插件
2010/11/19 Javascript
检测jQuery.js是否已加载的判断代码
2011/05/20 Javascript
自定义jquery模态窗口插件无法在顶层窗口显示问题
2014/05/29 Javascript
JS定义网页表单提交(submit)的方法
2015/03/20 Javascript
js实现同一个页面多个渐变效果的方法
2015/04/10 Javascript
H5用户注册表单页 注册模态框!
2016/09/17 Javascript
运用js教你轻松制作html音乐播放器
2020/04/17 Javascript
防止重复发送 Ajax 请求
2017/02/15 Javascript
Angular4 组件通讯方法大全(推荐)
2018/07/12 Javascript
解决vuex数据异步造成初始化的时候没值报错问题
2019/11/13 Javascript
vue组件传值的实现方式小结【三种方式】
2020/02/05 Javascript
JavaScript实现随机点名器
2020/03/25 Javascript
Django框架中数据的连锁查询和限制返回数据的方法
2015/07/17 Python
python处理csv数据动态显示曲线实例代码
2018/01/23 Python
在Python中使用defaultdict初始化字典以及应用方法
2018/10/31 Python
Django框架实现的普通登录案例【使用POST方法】
2019/05/15 Python
django中SMTP发送邮件配置详解
2019/07/19 Python
pytorch打印网络结构的实例
2019/08/19 Python
html5 localStorage本地存储_动力节点Java学院整理
2017/07/06 HTML / CSS
香港个人化生活购物网站:Ballyhoo Limited
2016/09/10 全球购物
优瑞自动咖啡机官网:Jura
2018/09/29 全球购物
泰国国际航空公司官网:Thai Airways International
2019/12/04 全球购物
劳动之星获奖感言
2014/02/01 职场文书
中青班党性分析材料
2014/02/16 职场文书
献爱心捐款倡议书
2014/05/14 职场文书
班级学习雷锋活动总结
2014/07/04 职场文书
大学奖学金获奖感言
2014/08/15 职场文书
实习证明格式范文
2014/10/14 职场文书
机关工会工作总结2015
2015/05/26 职场文书
参加招聘会后的感想
2015/08/10 职场文书
简单介绍 http请求响应参数、无连接无状态、MIME、状态码、端口、telnet、curl
2021/03/31 HTML / CSS
win10安装配置nginx的过程
2021/03/31 Servers