这样写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 Flask实现restful api service
Dec 04 Python
python实现自动获取IP并发送到邮箱
Dec 26 Python
Django组件cookie与session的具体使用
Jun 05 Python
Python使用sklearn库实现的各种分类算法简单应用小结
Jul 04 Python
python3中替换python2中cmp函数的实现
Aug 20 Python
tensorflow 实现打印pb模型的所有节点
Jan 23 Python
使用python实现微信小程序自动签到功能
Apr 27 Python
Python selenium模拟手动操作实现无人值守刷积分功能
May 13 Python
PyTorch安装与基本使用详解
Aug 31 Python
python 写一个水果忍者游戏
Jan 13 Python
90行Python代码开发个人云盘应用
Apr 20 Python
Pytest中skip和skipif的具体使用方法
Jun 30 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获取远程图片并把它保存到本地的代码
2008/04/07 PHP
php使用mb_check_encoding检查字符串在指定的编码里是否有效
2013/11/07 PHP
PHP 导出Excel示例分享
2014/08/18 PHP
PHP中Header使用的HTTP协议及常用方法小结
2014/11/04 PHP
php 利用socket发送HTTP请求(GET,POST)
2015/08/24 PHP
利用PHP如何写APP接口详解
2016/08/23 PHP
ThinkPHP 模板引擎使用详解
2017/05/07 PHP
PHP实现使用DOM将XML数据存入数组的方法示例
2017/09/27 PHP
JavaScript之IE的fireEvent方法详细解析
2013/11/20 Javascript
javascript中处理时间戳为日期格式的方法
2014/01/02 Javascript
Jquery性能优化详解
2014/05/15 Javascript
jquery中$(#form :input)与$(#form input)的区别
2014/08/18 Javascript
jQuery基于ajax实现星星评论代码
2015/08/07 Javascript
JavaScript实现将文本框的值插入指定位置的方法
2015/08/13 Javascript
盘点javascript 正则表达式中 中括号的【坑】
2016/03/16 Javascript
jQuery 中的 DOM 操作
2016/04/26 Javascript
基于JavaScript实现轮播图原理及示例
2020/04/10 Javascript
详解vue-cli项目中怎么使用mock数据
2018/05/29 Javascript
JS实现的获取银行卡号归属地及银行卡类型操作示例
2019/01/08 Javascript
解决Layui数据表格显示无数据提示的问题
2019/11/14 Javascript
[55:03]LGD vs EG 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
[49:35]KG vs SECRET 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
Python的装饰器模式与面向切面编程详解
2015/06/21 Python
python 自定义装饰器实例详解
2019/07/20 Python
python deque模块简单使用代码实例
2020/03/12 Python
python实现用户名密码校验
2020/03/18 Python
LA MER海蓝之谜美国官网:传奇面霜
2016/08/27 全球购物
施华洛世奇天猫官方旗舰店:SWAROVSKI
2017/04/17 全球购物
微软日本官方网站:Microsoft日本
2017/11/26 全球购物
Foot Locker德国官方网站:美国运动服和鞋类零售商
2018/11/01 全球购物
毕业生求职找工作的自我评价范文
2013/11/27 职场文书
银行办公室岗位职责
2014/03/10 职场文书
安全保证书范文
2014/04/29 职场文书
公司担保书格式范文
2014/05/12 职场文书
高中校园广播稿
2014/10/21 职场文书
介绍一下28个JS常用数组方法
2022/05/06 Javascript