is and == (is not other_thread)
Remco Gerlich
scarblac at pino.selwerd.nl
Thu Mar 1 08:04:30 EST 2001
Grant Griffin <not.this at seebelow.org> wrote in comp.lang.python:
> I've had a few of my own little questions about "is" and "==", vis-a-vis
> strings:
>
> 1) Are two strings that are equal in the "==" sense always guaranteed to
> be equal in the "is" sense?
No. Only when they have been interned. No guarantees about interning are
given, but you can use intern() yourself, I think string literals are
automatically interned, and maybe short strings as well. But no, you can't
in general use 'is' for string comparison.
> 2) Is "is" faster than "==" for strings? Of hand, it would seem so.
> That's why I'm tempted to use it for string equality. (OTOH, if strings
> can be "==" equal without being "is" equal, than I guess that's wildly
> false economy. <wink>)
'is' may be slightly faster, but can't be used in general.
> 3) Internally, does "==" for strings always begin with an "is"
> operation?
'==' starts with an 'is' test for any argument.
Simple test:
class A:
def __cmp__(self, other):
return -1 # Compares different to anything
print A() == A() # Prints 0, two different instances
x = A()
print x == x # Prints 1, the 'is' test is used and __cmp__ is skipped
> (btw,-nobody-ever-answers-every-part-of-my-silly-multipart-questions,
> -so-i-dare-you-folks-to-buck-the-trend-<wink>)-ly y'rs,
Oops. It was an accident, honest! I hadn't read this yet!
--
Remco Gerlich
More information about the Python-list
mailing list