def complex(a=1)
是对的。每个导入应该独占一行
导入应该在文件顶部。导入顺序应按照通用到不通用顺序,标准库大于第三方库大于程序库
Yes:if foo: bar(foo)
No:if foo: bar(foo)else: baz(foo)try: bar(foo)except ValueError: baz(foo)try:bar(foo)except ValueError: baz(foo)
在文件和sockets结束时, 显式的关闭它.
除文件按外,sockets或其他类似文件的对象在没有必要的情况下打开,会有很多副作用,例如:
with open('q.txt') as f:for line in f:print(line)
f.closed()
import contextlibwith contextlib.closing(urllib.urlopen("http://www.python.org/")) as front_page:for line in front_page:print line
/**
* 文件描述,他的使用和必要信息
* 存在的依赖
* 作者
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_spamself.eggs = 0def public_method(self):"""Performs operation blah."""
def fetch_bigtable_rows(big_table, keys, other_silly_variable=None):"""方法内容Retrieves rows pertaining to the given keys from the Table instancerepresented by big_table. Silly things may happen ifother_silly_variable is not None.参数:big_table: An open Bigtable Table instance.keys: A sequence of strings representing the key of each table rowto fetch.other_silly_variable: Another optional variable, that has a muchlonger name than the other args, and which does nothing.Returns:A dict mapping keys to the corresponding table row datafetched. Each row is represented as a tuple of strings. Forexample:{'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
/**
* 参数描述
* @type {string} 值描述
/** */ (x)
Yes: # See details at# http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.html
No: # See details at# http://www.example.com/us/developer/documentation/api/content/\# v2.0/csv_file_name_extension_full_specification.html
最需要写注释的是代码中那些技巧性的部分. 如果你在下次 代码审查 的时候必须解释一下, 那么你应该现在就给它写注释. 对于复杂的操作, 应该在其操作开始前写上若干行注释. 对于不是一目了然的代码, 应在其行尾添加注释.
# We use a weighted dictionary search to find out where i is in
# the array. We extrapolate position based on the largest num
# in the array and the array size and then do binary search to
# get the exact number.if i & (i-1) == 0: # True if i is 0 or a power of 2.
# TODO(kl@gmail.com): Use a "*" here for string repetition.
# TODO(Zeke) Change this to use relations.