Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c78c1f7

Browse files
committed
we've met somewhere, i remember ..
1 parent 05ad45b commit c78c1f7

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

‎README.md

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,10 @@ Imported names should always be considered an implementation detail. Other modul
611611

612612
* 代码不应该以一种不利于其他 python 实现(PyPy, Jython, IronPython, Cython, Psyco 诸如此类)的方式编写。 例如:不要使用 a += b 或 a = a + b 来实现就地字符串连接,在库的性能敏感部分,应该使用 ''.join() 的形式,这就能保证在不同的 python 实现中,连接动作可以在线性时间内完成。
613613

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,那可就完蛋了
615615

616616
* 尽管功能相同,从可读性上考虑:
617+
617618
```python
618619
是:
619620

@@ -626,7 +627,12 @@ Imported names should always be considered an implementation detail. Other modul
626627

627628
* 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.
628629

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+
629634
* 使用 def 语句而不用赋值语句直接绑定一个 lambda 表达式到标识符上:
635+
630636
```python
631637
是:
632638

@@ -636,33 +642,47 @@ Imported names should always be considered an implementation detail. Other modul
636642

637643
f = lambda x: 2*x
638644
```
645+
639646
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
640647

641648
* 捕获的异常要说明 "错误出在哪里了 ?" 而不是仅仅说明 "哎呀!出问题了!"
642649

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 时保留属性名称,或将原始异常的文本嵌入到新的异常消息中)。
653+
654+
* 当在 Python 2 中抛出异常时,使用 <code>raise ValueError('message')</code> 而不是老式的 <code>raise ValueError, 'message'</code>,后者已经在 Python 3 中废弃。由于使用了括号,可以避免行连续符的使用
644655

645-
* 当在 Python 2 中抛出异常时,使用 <code>raise ValueError('message')</code> 而不用 <code>raise ValueError, 'message'</code>,这样可以避免行连续符的使用
656+
* 当捕获异常时,尽可能提及具体的异常而不是使用一个赤裸裸的 except 子句。一个裸露的 except: 子句将捕获 SystemExit 和 KeyboardInterrupt 异常,这样的话就难于使用 control-c 中断程序,并可能掩盖其他问题。如果想要捕获标志程序错误的所有异常的话,用 except Exception:(裸露的 except 子句等同于 except BaseException:):
646657

647-
* 当捕获异常时,尽可能提及具体的异常而不是使用一个裸露的 except 子句,例如:
648658
```python
649659
try:
650660
import platform_specific_module
651661
except ImportError:
652662
platform_specific_module = None
653663
```
654664

655-
* 当对捕获的异常重命名时,使用以下语法:
665+
<blockquote>
666+
一个很好的经验法则是将裸露的 except 子句仅用于以下两种情况:
667+
668+
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.
671+
</blockquote>
672+
673+
* 当对捕获的异常重命名时,使用 2.6 版本引入的语法:
674+
656675
```python
657676
try:
658677
process_data()
659678
except Exception as exc:
660679
raise DataProcessingFailedError(str(exc))
661680
```
662681

663-
* 当捕获操作系统错误时,使用 Python 3.3 中介绍的异常层次结构
682+
* 当捕获操作系统错误时,相对于内置的 errno 值,最好是使用 Python 3.3 中介绍的显式异常层次结构
683+
684+
* 对于所有的 try/except 子句,将 try 子句限制为必需的绝对最小代码量避免隐藏 bug:
664685

665-
* 对于所有的 try/except 子句,将 try 子句限制为必需的绝对最小代码量避免隐藏错误
666686
```python
667687
是:
668688

@@ -683,9 +703,11 @@ except Exception as exc:
683703
return key_not_found(key)
684704
```
685705

686-
* 特定代码块的本地资源使用 with 语句确保使用后立即释放,try/finally 也可以
706+
* 特定代码块的本地资源使用 with 语句确保使用后立即释放,不能自动释放的使用 try/finally 也可以
687707

708+
<blockquote>TODO:</blockquote>
688709
* 除了申请和释放资源,任何时候都应该使用单独的函数和方法调用 Context managers,例如:
710+
689711
```python
690712
是:
691713

@@ -698,7 +720,7 @@ except Exception as exc:
698720
do_stuff_in_transaction(conn)
699721
```
700722

701-
* 返回值要一致,be consistent:
723+
* 函数返回语句要一致。在一个函数内的所有返回语句要么都返回一个表达式,要么都不返回。如果任何一个返回语句返回了表达式,那么其他任何没有返回值的语句应该明确声明为 return None。并且在函数结束部分必须出现返回语句:
702724
```python
703725
是:
704726

@@ -725,25 +747,31 @@ except Exception as exc:
725747
return math.sqrt(x)
726748
```
727749

728-
* 使用 string 方法而不是 string 模块。it's faster。当然,除了 2.0 版本之前 python 代码的向后兼容性
750+
* 相对于 string 模块,使用 string 方法要快的多并且与 unicode strings 共享相同的 API。当然了,除了需要考虑 2.0 版本之前 python 代码向后兼容性的情况
729751

730752
* 使用 ''.startswith() 和 ''.endswith() 而不是字符串切片来检查前缀或后缀,例如:
753+
731754
```python
732755
是: if foo.startswith('bar'):
733756
否: if foo[:3] == 'bar':
734757
```
735758

736-
* 对象类型比较因该使用isinstance() 而不是直接比较,例如:
759+
* 对象类型比较应该使用isinstance() 而不是直接比较:
760+
737761
```python
738762
是: if isinstance(obj, int):
739763
否: if type(obj) is type(1):
740764
```
741-
在 Python 2 中,string 和 unicode 公共基类是 basestring,因此:
765+
766+
当检查一个对象是否为字符串时,一定要注意这个对象也可能是 unicode 字符串!在 Python 2 中,string 和 unicode 拥有一个公共基类 basestring,因此可以这么的:
767+
742768
```python
743769
if isinstance(obj, basestring):
744770
```
745771

746-
* 对于序列(字符串,列表,元组),空的序列是 false:
772+
在 Python 3 中,unicode 和 basestring 已经不复存在了(there's only str),并且 bytes object 也不再视为一种 string 了,而是一个整形序列
773+
774+
* 对于序列(字符串,列表,元组)的判空操作:
747775
```python
748776
是:
749777
if not seq:
@@ -756,14 +784,21 @@ if isinstance(obj, basestring):
756784

757785
* 不要使用尾随空格
758786

759-
* 不要使用 == 比较布尔值:
787+
* 不要使用 == 验证布尔值为 Ture 或 False:
788+
760789
```python
761790
是:
762791
if greeting:
763792
否:
764793
if greeting == True:
765-
糟糕:
794+
虾扯蛋:
766795
if greeting is True:
767796
```
768797

798+
799+
800+
801+
802+
803+
769804
## --- 别扯了,再扯蛋都碎了 ---

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /