2014-10-27 23 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

相关问题