婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 這樣寫python注釋讓代碼更加的優雅

這樣寫python注釋讓代碼更加的優雅

熱門標簽:北瀚ai電銷機器人官網手機版 北京電銷外呼系統加盟 地圖標注面積 佛山400電話辦理 市場上的電銷機器人 儋州電話機器人 朝陽手機外呼系統 小蘇云呼電話機器人 所得系統電梯怎樣主板設置外呼

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代碼注釋規范代碼實例解析
  • 將python代碼和注釋分離的方法
  • python代碼如何注釋
  • 用python統計代碼行的示例(包括空行和注釋)

標簽:寧夏 江蘇 定西 金融催收 龍巖 商丘 云南 酒泉

巨人網絡通訊聲明:本文標題《這樣寫python注釋讓代碼更加的優雅》,本文關鍵詞  這樣,寫,python,注釋,讓,代碼,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《這樣寫python注釋讓代碼更加的優雅》相關的同類信息!
  • 本頁收集關于這樣寫python注釋讓代碼更加的優雅的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 德钦县| 大埔县| 迁安市| 屏南县| 肃宁县| 图木舒克市| 冀州市| 安达市| 高阳县| 南京市| 天柱县| 高平市| 涿州市| 彭州市| 丰城市| 甘孜| 镇雄县| 沙河市| 巴林右旗| 灵丘县| 灵丘县| 临澧县| 漾濞| 连州市| 荆州市| 财经| 永靖县| 东台市| 宜兰县| 柞水县| 汉源县| 景谷| 海阳市| 车险| 巴塘县| 稷山县| 汨罗市| 双峰县| 宿松县| 元谋县| 广州市|