2014-10-27 27 views
1

例如:什麼語法對Python更好?=或者不是?

match = re.search('...', str, re.IGNORECASE) 
if match is not None: 
    pass 

# or 
if match != None: 
    pass 

什麼是好?

+0

'匹配不是無' – 2014-10-27 18:49:03

+3

既不是更好,因爲它們的行爲不同。 – rightfold 2014-10-27 18:49:43

+1

'is'是一個身份運算符,所以它應該只在檢查對象是否具有_same_內存地址時使用(單例如無限定)。要小心,因爲'hello'=='hello',但'hello'不是'hello'' – polvoazul 2014-10-27 18:55:18

回答

5

PEP 8

比較像無單身應始終isis not,從來沒有平等的運營商來完成。

+0

哇.. 好的,非常感謝! – avasin 2014-10-27 18:52:15

+0

@AaronHall你可以創建自己的singletons http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern http://stackoverflow.com/questions/31875/is-there-a-simple-優雅的方式來定義單身在Python中 – 2014-10-27 19:06:27

+0

這可能會更好,如果添加到您的答案,以免它被解釋不同。 – 2014-10-27 19:42:31

相關問題