views:

241

answers:

5

... the is keyword that can be used for equality in strings.

>>> s = 'str'
>>> s is 'str'
True
>>> s is 'st'
False

I tried both __is__() and __eq__() but they didn't work.

>>> class MyString:
...   def __init__(self):
...     self.s = 'string'
...   def __is__(self, s):
...     return self.s == s
...
>>>
>>>
>>> m = MyString()
>>> m is 'ss'
False
>>> m is 'string' # <--- Expected to work
False
>>>
>>> class MyString:
...   def __init__(self):
...     self.s = 'string'
...   def __eq__(self, s):
...     return self.s == s
...
>>>
>>> m = MyString()
>>> m is 'ss'
False
>>> m is 'string' # <--- Expected to work, but again failed
False
>>>

Thanks for your help!

+11  A: 

Testing strings with is only works when the strings are interned. Unless you really know what you're doing and explicitly interned the strings you should never use is on strings.

is tests for identity, not equality. That means Python simply compares the memory address a object resides in. is basically answers the question "Do I have two names for the same object?" - overloading that would make no sense.

For example, ("a" * 100) is ("a" * 100) is False. Usually Python writes each string into a different memory location, interning mostly happens for string literals.

THC4k
I've observed in the past that string interning may happen for run-time computed and input values if they're sufficiently short. 'a' * 100 is not 'a' * 100; but 'a' * 20 is "a" * 20. Meanwhile 'a'.upper() is not 'a'.upper().Jython, IronPython, PyPy and others may intern more agressively.In short it's implementation dependent. Calling the 'intern()' function on strings will "force" a string to have the same object identity as any equivalent and previously intern()'d string, as you say. However, I don't know of a valid use case for testing string identity. (Possible performance aside).
Jim Dennis
+1  A: 

The is keyword compares objects (or, rather, compares if two references are to the same object).

Which is, I think, why there's no mechanism to provide your own implementation.

It happens to work sometimes on strings because Python stores strings 'cleverly', such that when you create two identical strings they are stored in one object.

>>> a = "string"
>>> b = "string"
>>> a is b
True
>>> c = "str"+"ing"
>>> a is c
True

You can hopefully see the reference vs data comparison in a simple 'copy' example:

>>> a = {"a":1}
>>> b = a
>>> c = a.copy()
>>> a is b
True
>>> a is c
False
pycruft
+3  A: 

The is operator is equivalent to comparing id(x) values. id is currently implemented to use pointers as the comparison. So you can't overload is itself, and AFAIK you can't overload id either.

So, you can't. Unusual in python, but there it is.

Phil H
+1  A: 

The Python is keyword tests object identity. You should NOT use it to test for string equality. It may seem to work frequently because Python implementations, like those of many very high level languages, performs "interning" of strings. That is to say that string literals and values are internally kept in a hashed list and those which are identical are rendered as references to the same object. (This is possible because Python strings are immutable).

However, as with any implementation detail, you should not rely on this. If you want to test of equality use the == operator. If you truly want to test for object identity then use is --- and I'd be hard-pressed to come up with a case where you should care about string object identity. Unfortunately you can't count on whether two strings are somehow "intentionally" identical object references because of the aforementioned interning.

Jim Dennis
the only place in Python where you want to do identity comparison is when comparing to Singletons (e.g. None) and sentinel values that needs to be unique. Other than that, there is probably almost no reason for is.
Lie Ryan
@Lie Ryan: I tend to agree. I only ever use it for None and for special sentinels that I've created (usually as calls to the base 'object()'). However, I don't feel comfortable asserting that there's no other valid uses for the 'is' operator; just none that I can think of. (Possibly a testament to my own ignorance).
Jim Dennis
A: 

If you are not afraid of messing up with bytecode, you can intercept and patch COMPARE_OP with 8 ("is") argument to call your hook function on objects being compared. Look at dis module documentation for start-in.

And don't forget to intercept __builtin__.id() too if someone will do id(a) == id(b) instead of a is b.

modchan