@@ -136,7 +136,7 @@ assert 语句也是如此。
136136算法和程序设计技术先驱,计算机排版系统 TEX 和 METAFONT 的发明者 Donald Knuth,推荐使用以下形式:
137137
138138``` Python
139- # Yes: easy to match operators with operands
139+ # 是: easy to match operators with operands
140140income = (gross_wages
141141 + taxable_interest
142142 + (dividends - qualified_dividends)
@@ -165,11 +165,11 @@ Python 2 默认ASCII,Python 3 默认UTF-8。
165165<h3 id =" 3.7 " >模块导入</h3 >
166166
167167``` Python
168- YES : from subprocess import Popen, PIPE
168+ 是: from subprocess import Popen, PIPE
169169 import os
170170 import sys
171171
172- NO : import sys, os
172+ 否: import sys, os
173173```
174174
175175模块导入总是位于文件顶部,在模块注释和文档字符串之后,模块全局变量和常量之前。
@@ -248,40 +248,40 @@ import sys
248248在以下场景避免不必要的空格
249249
250250``` Python
251- Yes: spam(ham[1 ], {eggs: 2 })
252- No: spam( ham[ 1 ], { eggs: 2 } )
251+ 是: spam(ham[1 ], {eggs: 2 })
252+ 否: spam( ham[ 1 ], { eggs: 2 } )
253253
254- Yes: foo = (0 ,)
255- No: bar = (0 , )
254+ 是: foo = (0 ,)
255+ 否: bar = (0 , )
256256
257- Yes: if x == 4 : print x, y; x, y = y, x
258- No: if x == 4 : print x , y ; x , y = y , x
257+ 是: if x == 4 : print x, y; x, y = y, x
258+ 否: if x == 4 : print x , y ; x , y = y , x
259259
260- Yes:
260+ 是:
261261ham[1 :9 ], ham[1 :9 :3 ], ham[:9 :3 ], ham[1 ::3 ], ham[1 :9 :]
262262ham[lower:upper], ham[lower:upper:], ham[lower::step]
263263ham[lower+ offset : upper+ offset]
264264ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
265265ham[lower + offset : upper + offset]
266266
267- No:
267+ 否:
268268ham[lower + offset:upper + offset]
269269ham[1 : 9 ], ham[1 :9 ], ham[1 :9 :3 ]
270270ham[lower : : upper]
271271ham[ : upper]
272272
273- Yes: spam(1 )
274- No: spam (1 )
273+ 是: spam(1 )
274+ 否: spam (1 )
275275
276- Yes: dct[' key' ] = lst[index]
277- No: dct [' key' ] = lst [index]
276+ 是: dct[' key' ] = lst[index]
277+ 否: dct [' key' ] = lst [index]
278278
279- Yes:
279+ 是:
280280x = 1
281281y = 2
282282long_variable = 3
283283
284- No:
284+ 否:
285285x = 1
286286y = 2
287287long_variable = 3
@@ -294,15 +294,15 @@ long_variable = 3
294294在二元运算符周围使用空格:
295295
296296``` python
297- Yes:
297+ 是:
298298
299299i = i + 1
300300submitted += 1
301301x = x* 2 - 1
302302hypot2 = x* x + y* y
303303c = (a+ b) * (a- b)
304304
305- No:
305+ 否:
306306
307307i= i+ 1
308308submitted += 1
@@ -313,24 +313,24 @@ c = (a + b) * (a - b)
313313表示关键字参数货默认参数值时,不要使用空格:
314314
315315``` python
316- Yes:
316+ 是:
317317
318318def complex (real , imag = 0.0 ):
319319 return magic(r = real, i = imag)
320- No:
320+ 否:
321321
322322def complex (real , imag = 0.0 ):
323323 return magic(r = real, i = imag)
324324```
325325函数注释的场景:
326326
327327``` python
328- Yes:
328+ 是:
329329
330330def munge (input : AnyStr): ...
331331def munge () -> AnyStr: ...
332332
333- No:
333+ 否:
334334
335335def munge (input :AnyStr): ...
336336def munge ()->PosInt: ...
@@ -339,12 +339,12 @@ def munge()->PosInt: ...
339339当参数注释和默认值共存时:
340340
341341``` python
342- Yes:
342+ 是:
343343
344344def munge (sep : AnyStr = None ): ...
345345def munge (input : AnyStr, sep : AnyStr = None , limit = 1000 ): ...
346346
347- No:
347+ 否:
348348
349349def munge (input : AnyStr= None ): ...
350350def munge (input : AnyStr, limit = 1000 ): ...
@@ -353,7 +353,7 @@ def munge(input: AnyStr, limit = 1000): ...
353353复合声明不建议使用:
354354
355355``` python
356- Yes:
356+ 是:
357357
358358if foo == ' blah' :
359359 do_blah_thing()
@@ -394,7 +394,7 @@ if foo == 'blah': one(); two(); three()
394394单元素元组强制使用逗号:
395395
396396``` python
397- Yes:
397+ 是:
398398
399399FILES = (' setup.cfg' ,)
400400
@@ -404,7 +404,7 @@ FILES = 'setup.cfg',
404404```
405405当使用版本控制系统时,一组希望后续扩展的值/参数/改善的条目使用以下形式:
406406``` python
407- Yes:
407+ 是:
408408
409409FILES = [
410410 ' setup.cfg' ,
@@ -413,7 +413,7 @@ FILES = [
413413initialize(FILES ,
414414 error = True ,
415415 )
416- No:
416+ 否:
417417
418418FILES = [' setup.cfg' , ' tox.ini' ,]
419419initialize(FILES , error = True ,)
@@ -595,11 +595,11 @@ Imported names should always be considered an implementation detail. Other modul
595595
596596* 尽管功能相同,从可读性上考虑:
597597``` python
598- Yes:
598+ 是:
599599
600600if foo is not None :
601601
602- No:
602+ 否:
603603
604604if not foo is None :
605605```
@@ -608,11 +608,11 @@ if not foo is None:
608608
609609* 使用 def 语句而不是赋值语句直接绑定一个 lambda 表达式到标志符上:
610610``` python
611- Yes:
611+ 是:
612612
613613def f (x ): return 2 * x
614614
615- No:
615+ 否:
616616
617617f = lambda x : 2 * x
618618```
@@ -644,7 +644,7 @@ except Exception as exc:
644644
645645* 对于所有的 try/except 子句,将 try 子句限制为必需的绝对最小代码量避免隐藏错误
646646``` python
647- Yes:
647+ 是:
648648
649649try :
650650 value = collection[key]
@@ -653,7 +653,7 @@ except KeyError:
653653else :
654654 return handle_value(value)
655655
656- No:
656+ 否:
657657
658658try :
659659 # Too broad!
@@ -667,20 +667,20 @@ except KeyError:
667667
668668* 除了申请和释放资源,任何时候都应该使用单独的函数和方法调用 Context managers,e.g:
669669``` python
670- Yes:
670+ 是:
671671
672672with conn.begin_transaction():
673673 do_stuff_in_transaction(conn)
674674
675- No:
675+ 否:
676676
677677with conn:
678678 do_stuff_in_transaction(conn)
679679```
680680
681681* 返回值要一致,be consistent:
682682```
683- Yes:
683+ 是:
684684
685685def foo(x):
686686 if x >= 0:
@@ -693,7 +693,7 @@ def bar(x):
693693 return None
694694 return math.sqrt(x)
695695
696- No:
696+ 否:
697697
698698def foo(x):
699699 if x >= 0:
@@ -709,15 +709,15 @@ def bar(x):
709709
710710* 使用 ''.startswith() 和 ''.endswith() 而不是字符串切片来检查前缀或后缀,e.g:
711711``` python
712- Yes: if foo.startswith(' bar' ):
713- No: if foo[:3 ] == ' bar' :
712+ 是: if foo.startswith(' bar' ):
713+ 否: if foo[:3 ] == ' bar' :
714714```
715715
716716* 对象类型比较因该使用isinstance() 而不是直接比较,e.g:
717717``` python
718- Yes: if isinstance (obj, int ):
718+ 是: if isinstance (obj, int ):
719719
720- No: if type (obj) is type (1 ):
720+ 否: if type (obj) is type (1 ):
721721```
722722在 Python 2 中,string 和 unicode 公共基类是 basestring,因此:
723723``` python
@@ -726,20 +726,20 @@ if isinstance(obj, basestring):
726726
727727* 对于序列(字符串,列表,元组),空的序列是 false:
728728``` python
729- Yes: if not seq:
730- if seq:
729+ 是: if not seq:
730+ if seq:
731731
732- No: if len (seq):
732+ 否: if len (seq):
733733 if not len (seq):
734734```
735735
736736* 不要使用尾随空格
737737
738738* 不要使用 == 比较布尔值:
739739``` python
740- Yes: if greeting:
741- No: if greeting == True :
742- Worse : if greeting is True :
740+ 是: if greeting:
741+ 否: if greeting == True :
742+ 糟糕 : if greeting is True :
743743```
744744
745745<h2 id =" 9.1 " >函数注释</h2 >
0 commit comments