#-*-coding:utf-8-*-標(biāo)識(shí)每行代碼盡量不超過 80 個(gè)字符(在特殊情況下可以略微超過 80 ,但最長(zhǎng)不得超過 120)
理由:
簡(jiǎn)單說,自然語(yǔ)言使用雙引號(hào),機(jī)器標(biāo)示使用單引號(hào),因此 代碼里 多數(shù)應(yīng)該使用 單引號(hào)
"..."u"你好世界"'...'
例如 dict 里的 keyr"...""""......"""class A:
def __init__(self):
pass
def hello(self):
pass
def main():
pass
#-*-conding:utf-8-*-標(biāo)識(shí)# 正確的寫法
import os
import sys
# 不推薦的寫法
import sys,os
# 正確的寫法
from subprocess import Popen, PIPE
# 正確的寫法
from foo.bar import Bar
# 不推薦的寫法
from ..bar import Bar
import os
import sys
import msgpack
import zmq
import foo
from myclass import MyClass
import bar
import foo.bar
bar.Bar()
foo.bar.Bar()
[=,-,+=,==,>,in,is not, and]:# 正確的寫法
i = i + 1
submitted += 1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
# 不推薦的寫法
i=i+1
submitted +=1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
,之后要有空格# 正確的寫法
def complex(real, imag):
pass
# 不推薦的寫法
def complex(real,imag):
pass
# 正確的寫法
def complex(real, imag=0.0):
pass
# 不推薦的寫法
def complex(real, imag = 0.0):
pass
# 正確的寫法
spam(ham[1], {eggs: 2})
# 不推薦的寫法
spam( ham[1], { eggs : 2 } )
# 正確的寫法
dict['key'] = list[index]
# 不推薦的寫法
dict ['key'] = list [index]
# 正確的寫法
x = 1
y = 2
long_variable = 3
# 不推薦的寫法
x = 1
y = 2
long_variable = 3
Python 支持括號(hào)內(nèi)的換行。這時(shí)有兩種情況。
1) 第二行縮進(jìn)到括號(hào)的起始處
foo = long_function_name(var_one, var_two,
var_three, var_four)
2) 第二行縮進(jìn) 4 個(gè)空格,適用于起始括號(hào)就換行的情形
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
使用反斜杠\換行,二元運(yùn)算符+ .等應(yīng)出現(xiàn)在行末;長(zhǎng)字符串也可以用此法換行
session.query(MyTable).\
filter_by(id=1).\
one()
print 'Hello, '\
'%s %s!' %\
('Harry', 'Potter')
禁止復(fù)合語(yǔ)句,即一行中包含多個(gè)語(yǔ)句:
# 正確的寫法
do_first()
do_second()
do_third()
# 不推薦的寫法
do_first();do_second();do_third();
if/for/while一定要換行:
# 正確的寫法
if foo == 'blah':
do_blah_thing()
# 不推薦的寫法
if foo == 'blah': do_blash_thing()
docstring 的規(guī)范中最其本的兩點(diǎn):
"""Return a foobar
Optional plotz says to frobnicate the bizbaz first.
"""
"""Oneline docstring"""