On Sat, 07 Nov 2009 14:22:28 -0800, sturlamolden wrote: > On 6 Nov, 14:35, "Alf P. Steinbach" <al...@start.no> wrote: > >> As I understand it, 'is' will always work and will always be efficient >> (it just checks the variable's type), while '==' can depend on the >> implementation of equality checking for the other operand's class. > > '==' checks for logical equality. 'is' checks for object identity.
So far so good, although technically == merely calls __eq__, which can be over-ridden to do (nearly) anything you like: >>> class Funny(object): ... def __eq__(self, other): ... return self.payload + other ... >>> f = Funny() >>> f.payload = 5 >>> f == 10 15 > None is a singleton of type NoneType. Since None evaluates to True only > when compared against itself, That's wrong. None never evaluates to True, it always evaluates as None, in the same way that 42 evaluates as 42 and [1,2,3] evaluates as [1,2,3]. Python literals evaluate as themselves, always. Perhaps you mean that *comparisons* of None evaluate to True only if both operands are None. That's incorrect too: >>> None > None False You have to specify the comparison. It would be a pretty strange language if both None==None and None!=None returned True. > it is safe to use both operators. Only if you want unexpected results if somebody passes the wrong sort of object to your code. >>> class NoneProxy: ... def __eq__(self, other): ... if other is None: return True ... return False ... >>> o = NoneProxy() >>> o is None False >>> o == None True You should use == *only* if you want to test for objects which are equal to None, *whatever that object may be*, and is if you want to test for None itself. -- Steven -- http://mail.python.org/mailman/listinfo/python-list