Thread: Python brevity
View Single Post
Old May 23rd, 2006, 8:16 AM   #11
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
(Gnaaah... take two)
Quote:
Originally Posted by DaWei
It wasn't directed at you, Arevos, it was directed at the rewriting of my code...
I apologise for the overbrevity of my comment. As Arevos has suggested, when you compare None (and to a lesser extent True and False) by value (==, !=) rather than by identity (is, is not) you can end up with unexpected results if the object you're comparing is buggy, etc. It's not a common problem, but when it happens it can be painful to track down, so it's better to have the habit of always using identity comparison rather than value comparison even if the object (in this case the regex matches) is (almost) certain to be bugfree.

The following should illustrate:
class Expected:
    pass

class Evil1:
    def __eq__(self, other):
        return self == other

class Evil2:
    def __cmp__(self, other):
        if other is self:
            return True
        return False

for cls in (Expected, Evil1, Evil2):
    x = cls()
    print cls.__name__
    print "   ", x == None, x != None, x is None, x is not None, "\n"
-T.
"One man's cargo cult is another man's defensive."
hydroxide is offline   Reply With Quote