美文网首页
番外篇:Python开发规范

番外篇:Python开发规范

作者: WESTWALL | 来源:发表于2018-05-15 17:58 被阅读0次

    原文链接:http://www.runoob.com/w3cnote/google-python-styleguide.html

    摘要

    行长度

    foo_bar(self, width, height, color='black', design=None, x='foo',
            emphasis=None, highlight=0)
    
    if (width == 0 and height == 0 and
        color == 'red' and emphasis == 'strong'):
        
    x = ('这是一个非常长非常长非常长非常长 '
         '非常长非常长非常长非常长非常长非常长的字符串')
    

    缩进

    # 与起始变量对齐
    foo = long_function_name(var_one, var_two,
                             var_three, var_four)
    
    # 字典中与起始值对齐
    foo = {
        long_dictionary_key: value1 +
                             value2,
        ...
    }
    
    # 4 个空格缩进,第一行不需要
    foo = long_function_name(
        var_one, var_two, var_three,
        var_four)
    
    # 字典中 4 个空格缩进
    foo = {
        long_dictionary_key:
            long_dictionary_value,
        ...
    }
    

    空格

    # 当'='用于指示关键字参数或默认参数值时, 不要在其两侧使用空格.
    def complex(real, imag=0.0): return magic(r=real, i=imag)
    

    Shebang

    大部分.py文件不必以#!作为文件的开始. 根据 PEP-394 , 程序的main文件应该以 #!/usr/bin/python2或者 #!/usr/bin/python3开始.

    文档注释

    接口注释:

    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
    

    类注释:

    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."""
    

    如果一个类不继承自其它类, 就显式的从object继承. 嵌套类也一样.

    class SampleClass(object):
        pass
    
    
    class OuterClass(object):
    
        class InnerClass(object):
            pass
    
    
    class ChildClass(ParentClass):
        """Explicitly inherits from another class already."""
    

    相关文章

      网友评论

          本文标题:番外篇:Python开发规范

          本文链接:https://www.haomeiwen.com/subject/tpeydftx.html