Python 中的注釋規范


在Python中,分為單行注釋,多行注釋,特殊注釋

特殊注釋:

  #!/usr/bin/env python

  這樣的注釋意思是:Python告訴LINUX 去哪個路徑下找Python的翻譯器

  #-*-coding:utf-8-*-

  這樣的注釋意思是: 告訴Python解釋器,如何解釋字符串中的編碼類型

多行注釋:/**/

  一般用於給類文檔,函數文檔作注釋

單行注釋:#

 

 

注釋要求:

 

  函數和方法注釋:

    Args; 列出每個參數的名字, 並在名字后使用一個冒號和一個空格, 分隔對該參數的描述.如果描述太長超過了單行80字符,使用2或者4個空格的懸掛縮進(與文件其他部分保持一致). 描述應該包括所需的類型和含義. 如果一個函數接受*foo(可變長度參數列表)或者**bar (任意關鍵字參數), 應該詳細列出*foo和**bar.

 

    Returns: (或者 Yields: 用於生成器)描述返回值的類型和語義. 如果函數返回None, 這一部分可以省略.

 

    Raises:列出與接口有關的所有異常.

    EXAMPLE:

      

def fetch_bigtable_rows(big_table, keys, other_silly_variable=None): """Fetches rows from a Bigtable.  Retrieves rows pertaining to the given keys from the Table instance  represented by big_table. Silly things may happen if  other_silly_variable is not None.  Args:  big_table: An open Bigtable Table instance.  keys: A sequence of strings representing the key of each table row  to fetch.  other_silly_variable: Another optional variable, that has a much  longer name than the other args, and which does nothing.  Returns:  A dict mapping keys to the corresponding table row data  fetched. Each row is represented as a tuple of strings. For  example:  {'Serak': ('Rigel VII', 'Preparer'),  'Zim': ('Irk', 'Invader'),  'Lrrr': ('Omicron Persei 8', 'Emperor')}  If a key from the keys argument is missing from the dictionary,  then that row was not found in the table.  Raises:  IOError: An error occurred accessing the bigtable.Table object.  """ pass

  類的注釋

    類應該在其定義下有一個用於描述該類的文檔字符串. 如果你的類有公共屬性(Attributes), 那么文檔中應該有一個屬性(Attributes)段. 並且應該遵守和函數參數相同的格式.

    

class SampleClass(object): """Summary of class here.  Longer class information....  Longer class information....  Attributes:  likes_spam: A boolean indicating if we like SPAM or not.  eggs: An integer count of the eggs we have laid.  """ def __init__(self, likes_spam=False): """Inits SampleClass with blah.""" self.likes_spam = likes_spam self.eggs = 0 def public_method(self): """Performs operation blah."""


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM