这样写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之eval()函数危险性浅析
Jul 03 Python
Python Tkinter简单布局实例教程
Sep 03 Python
Python查找相似单词的方法
Mar 05 Python
pygame学习笔记(3):运动速率、时间、事件、文字
Apr 15 Python
Python素数检测的方法
May 11 Python
python删除服务器文件代码示例
Feb 09 Python
基于python 微信小程序之获取已存在模板消息列表
Aug 05 Python
详解python路径拼接os.path.join()函数的用法
Oct 09 Python
将python2.7添加进64位系统的注册表方式
Nov 20 Python
Python 面向对象之类class和对象基本用法示例
Feb 02 Python
Python中的xlrd模块使用原理解析
May 21 Python
Python中无限循环需要什么条件
May 27 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版
2012/04/20 PHP
php判断字符串在另一个字符串位置的方法
2014/02/27 PHP
ThinkPHP实现多数据库连接的解决方法
2014/07/01 PHP
Yii把CGridView文本框换成下拉框的方法
2014/12/03 PHP
利用div+jquery自定义滚动条样式的2种方法
2013/07/18 Javascript
js语法学习之判断一个对象是否为数组
2014/05/13 Javascript
JavaScript+CSS实现仿Mootools竖排弹性动画菜单效果
2015/10/14 Javascript
EasyUI中在表单提交之前进行验证
2016/07/19 Javascript
jQuery leonaScroll 1.1 自定义滚动条插件(推荐)
2016/09/17 Javascript
半个小时学json(json传递示例)
2016/12/25 Javascript
Jquery uploadify 多余的Get请求(404错误)的解决方法
2017/01/26 Javascript
使用JS编写的随机抽取号码的小程序
2017/08/11 Javascript
基于vue组件实现猜数字游戏
2020/05/28 Javascript
写一个移动端惯性滑动&amp;回弹Vue导航栏组件 ly-tab
2018/03/06 Javascript
JavaScript事件发布/订阅模式原理与用法分析
2018/08/21 Javascript
vue-cli中安装方法(图文详细步骤)
2018/12/12 Javascript
vue项目中实现的微信分享功能示例
2019/01/21 Javascript
基于vue通用表单解决方案的思考与分析
2019/03/16 Javascript
浅谈vuex的基本用法和mapaction传值问题
2019/11/08 Javascript
python远程登录代码
2008/04/29 Python
python生成IP段的方法
2015/07/07 Python
在Django中创建第一个静态视图
2015/07/15 Python
Python读取和处理文件后缀为.sqlite的数据文件(实例讲解)
2017/06/27 Python
python八大排序算法速度实例对比
2017/12/06 Python
Flask实现图片的上传、下载及展示示例代码
2018/08/03 Python
Python函数基础实例详解【函数嵌套,命名空间,函数对象,闭包函数等】
2019/03/30 Python
python 爬虫百度地图的信息界面的实现方法
2019/10/27 Python
Python利用matplotlib绘制约数个数统计图示例
2019/11/26 Python
django配置app中的静态文件步骤
2020/03/27 Python
prAna官网:瑜伽、旅行和冒险服装
2019/03/10 全球购物
酒店总经理助理岗位职责
2014/02/01 职场文书
总经理秘书岗位职责
2014/03/17 职场文书
党员教师批评与自我批评发言稿
2014/10/15 职场文书
爱心募捐通知范文
2015/04/27 职场文书
2015年社区重阳节活动总结
2015/07/30 职场文书
电子表的操作介绍说明书
2019/10/28 职场文书