You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-15Lines changed: 50 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -611,9 +611,10 @@ Imported names should always be considered an implementation detail. Other modul
611
611
612
612
* 代码不应该以一种不利于其他 python 实现(PyPy, Jython, IronPython, Cython, Psyco 诸如此类)的方式编写。 例如:不要使用 a += b 或 a = a + b 来实现就地字符串连接,在库的性能敏感部分,应该使用 ''.join() 的形式,这就能保证在不同的 python 实现中,连接动作可以在线性时间内完成。
613
613
614
-
* 和例如 None 这类 singleton 的比较,应该使用 is 或 is not 而不是等号符。另外,小心使用 <code>if x</code> 如果你的本意是 <code>if x is not None</code>,如果 x 是个布尔变量值 false,就完蛋了。
614
+
* 和例如 None 这类 singleton 的比较,应该使用 is 或 is not 而不是 ==。另外,小心使用 <code>if x</code> 如果你的本意是 <code>if x is not None</code>,如果 x 是个布尔变量值 false,那可就完蛋了。
615
615
616
616
* 尽管功能相同,从可读性上考虑:
617
+
617
618
```python
618
619
是:
619
620
@@ -626,7 +627,12 @@ Imported names should always be considered an implementation detail. Other modul
626
627
627
628
* When implementing ordering operations with rich comparisons, it is best to implement all six operations ( \_\_eq\_\_ , \_\_ne\_\_ , \_\_lt\_\_ , \_\_le\_\_ , \_\_gt\_\_ , \_\_ge\_\_ ) rather than relying on other code to only exercise a particular comparison.
628
629
630
+
To minimize the effort involved, the functools.total_ordering() decorator provides a tool to generate missing comparison methods.
631
+
632
+
PEP 207 indicates that reflexivity rules are assumed by Python. Thus, the interpreter may swap y > x with x < y , y >= x with x <= y , and may swap the arguments of x == y and x != y . The sort() and min() operations are guaranteed to use the < operator and the max() function uses the > operator. However, it is best to implement all six operations so that confusion doesn't arise in other contexts.
633
+
629
634
* 使用 def 语句而不用赋值语句直接绑定一个 lambda 表达式到标识符上:
635
+
630
636
```python
631
637
是:
632
638
@@ -636,33 +642,47 @@ Imported names should always be considered an implementation detail. Other modul
636
642
637
643
f =lambdax: 2*x
638
644
```
645
+
639
646
The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression
640
647
641
648
* 捕获的异常要说明 "错误出在哪里了 ?" 而不是仅仅说明 "哎呀!出问题了!"
642
649
643
-
* 异常转移时,要讲详细的异常信息保留到新的异常中(Python 2: "raise X" Python 3: "raise X from Y")
650
+
* 正确使用异常链接。在 Python 3 中,应该使用 "raise X from Y" 来表示显式替换并且不会丢失原始追溯。
651
+
652
+
当有意替换一个内部异常(Python 2: "raise X", Python 3.3+: raise X from Non)时,请确保将相关的详细信息转移到新的异常(例如,将 KeyError 转换为 AttributeError 时保留属性名称,或将原始异常的文本嵌入到新的异常消息中)。
1、If the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred.
669
+
670
+
2、If the code needs to do some cleanup work, but then lets the exception propagate upwards with raise . try...finally can be a better way to handle this case.
0 commit comments