[Python-Dev] redefining is (original) (raw)

Guido van Rossum guido at python.org
Thu Mar 18 12:08:12 EST 2004


There is an idiom (I've seen it more in Lisp than in python) of creating a fresh object to act as a sentinel.

"done with this data" might well appear in the input, but the specific newly-created-string (which happens to look just like that) can't appear. The sentinal is usually a mutable object, but it is sometimes a string indicating the object's meaning. ("fail") It is surprising that some objects (like small integers) cannot be used, but I don't think the answer is to make the entire idiom unusable.

Sorry, if you're usign any immutable value there and expecting it to be a unique object, you're cruisin' for a bruisin', so to speak. The language spec explicitly allows but does not require the implementation to cache and reuse immutable values.

You could argue that they ought to be using (id(x) == id(y)) to emphasize that == isn't enough, but ... (x is y) seems just as clear, and the reference manual (5.9) says that is tests for object identity.

Please, just use None, [] or object() as a sentinel if you're going to compare using 'is'.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list