美文网首页
番外篇: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开发规范

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

  • 圆方圆python入门:如何学习(二)

    python如何学习(二) 一、编程开发规范 作为一门开发语言,python自然也有自己的编程规范,以下是特有且必...

  • Python开发规范

    Python风格规范 本项目包含了部分Google风格规范和PEP8规范,仅用作内部培训学习 命名规范 Pytho...

  • Ubuntu16.04系统 VSCode中python开发插件的

    VSCode中python开发插件的安装 1. python python插件提供了代码分析,高亮,规范化等很多基...

  • Python 开发编码规范

    最近,团队又来了几个小伙伴,经过一段时间磨合之后,发现彼此之间还是比较默契的,但有一个很大的问题是,每个人的编程风...

  • Python开发编码规范

    1.代码的缩进 4个空格一个缩进层次,永远不要混用制表符和空格. 最流行的Python缩进方式是仅使用空格, 其次...

  • python爬虫开发规范

    前言 实现一个爬虫需求很简单。但是管理很多爬虫是有以下难点的: 1.在其爬取的网页结构改变的时候。及时发现,且不因...

  • 移动前端开发规范(一般规范)

    系列目录 移动前端开发规范(一般规范)移动前端开发规范(技术栈规范)移动前端开发规范(HTML规范)移动前端开发规...

  • Python项目的开发规范

    开发环境准备 python 3.x - 版本3以上 pyCharm - 推荐的IDE pip3 - python的...

  • 2018-06-28 python命名规范

    Python命名规范 Google Python****命名规范 module_name, 模块 package...

网友评论

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

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