主頁(yè) > 知識(shí)庫(kù) > 這樣寫(xiě)python注釋讓代碼更加的優(yōu)雅

這樣寫(xiě)python注釋讓代碼更加的優(yōu)雅

熱門(mén)標(biāo)簽:銀行業(yè)務(wù) 美圖手機(jī) 呼叫中心市場(chǎng)需求 網(wǎng)站文章發(fā)布 服務(wù)器配置 智能手機(jī) 檢查注冊(cè)表項(xiàng) 鐵路電話系統(tǒng)

python這樣注釋?zhuān)屇愕拇a看起來(lái)更加的優(yōu)雅,是不是常常感覺(jué)自己的python代碼寫(xiě)出來(lái),看起來(lái)特別的亂,雖然可以正常運(yùn)行,但是在優(yōu)雅性上似乎欠缺的很多,這篇文章主要教你,如何讓你的python代碼看起來(lái)更加的優(yōu)雅與美觀,

一、注釋欣賞

這里有一段飛兔小哥哥自己常寫(xiě)的注釋模版

這里主要分為表頭注釋、類(lèi)注釋、歡迎語(yǔ)以及方法注釋

表頭注釋會(huì)標(biāo)注這個(gè)項(xiàng)目的名稱(chēng)、文件名、項(xiàng)目作者、時(shí)間等基礎(chǔ)信息

類(lèi)注釋會(huì)標(biāo)注這個(gè)類(lèi)主要用來(lái)做什么的

而方法注釋則表示當(dāng)前方法的作用

​​#!/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函數(shù)注釋規(guī)范

首先來(lái)兩段優(yōu)秀開(kāi)源框架的代碼注釋

例1 tornado.web.RequestHandler的get_arguments函數(shù).

 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函數(shù)

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)

對(duì)比下例1和例2, tornado框架的函數(shù)傾向與給出函數(shù)的用途說(shuō)明,而不提供具體的輸入?yún)?shù)說(shuō)明,并且相對(duì)來(lái)說(shuō)函數(shù)名字也是淺顯易懂,而requests庫(kù)看起來(lái)比較簡(jiǎn)潔一點(diǎn),具體的輸入和輸出都給的很完整,看起來(lái)很是賞心悅目,所以我個(gè)人更偏向于例2的注釋?zhuān)?dāng)然,也有將例1和例2注釋特點(diǎn)結(jié)合起來(lái)的庫(kù),比如tensorflow庫(kù),因?yàn)樯婕暗妮斎雲(yún)?shù)以及函數(shù)較為復(fù)雜,因此輸入?yún)?shù)和函數(shù)原理有較為詳盡的說(shuō)明??傊蟛糠志帉?xiě)函數(shù)的時(shí)候參考例2的注釋方式,代碼也看起來(lái)較為優(yōu)雅,而遇到比較復(fù)雜的情況,則可以參考例1加上必要的函數(shù)詳細(xì)說(shuō)明。

總結(jié)

到此這篇關(guān)于python注釋的文章就介紹到這了,更多相關(guān)python注釋內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python實(shí)現(xiàn)判斷一行代碼是否為注釋的方法
  • Python代碼注釋規(guī)范代碼實(shí)例解析
  • 將python代碼和注釋分離的方法
  • python代碼如何注釋
  • 用python統(tǒng)計(jì)代碼行的示例(包括空行和注釋)

標(biāo)簽:滄州 上海 樂(lè)山 紅河 長(zhǎng)治 河南 沈陽(yáng) 新疆

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《這樣寫(xiě)python注釋讓代碼更加的優(yōu)雅》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266