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註釋內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支援!
You may also like
相关贴文:
近期文章
- 免費在WhatsApp按鈕上添加訂單到WooCommerce | #WooCommerce#Whatsapp
- WooCommerce秘密功能您可以在2025年添加!
- 在2025年與WooCommerce商店在線賺錢
- WooCommerce可以進行訂閱嗎? – 精通應用
- crea il tuo negozio在線da zero con wooCommerce😱
- 如何使用Echorewards啟動推薦計劃
- 使用JetPack將WooCommerce網站連接到WooCommerce移動應用| #jetpack #woocommerce #app
- 🛒wooCommerce + AI🤖Zbuduj Sklep Internetowy krok po kroku 2024
- 成功付款後如何自動完成WooCommerce訂單
發佈留言