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

Jewett, Jim J jim.jewett at eds.com
Fri Mar 19 10:14:47 EST 2004


(me):

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

Greg Ewing:

In those rare cases where None is also a legitimate value, it's not hard to manufacture some unique object and test for it with 'is'. You just have to remember that strings and integers are not necessarily unique, which I don't think is too hard to keep in mind.

That is the (occasionally surprising) state today.

Since there is no intention of ever actually mutating the object, it is common (if mistaken) to assume that any new object -- even an immutable -- should work.

If we changed "is", it would be even more difficult to create a guaranteed unique object. Fortunately, Guido rejected the wholesale change.

I'm still a bit worried about a comment (which I can no longer find) suggesting a change for {}, so that

>>> {} is {}  
  True

I don't see this in 2.3, and would be very surprised (and disappointed) if it started to happen in 2.4, precisely because dictionaries are mutable objects that should work. Even worse,

>>> [] is []
False

was the recommended idiom; the difference between [] and {} as unique objects is far from clear. I personally like (and have used) Barry's suggestion of a new named object(). Unfortunately, object() is longer to type, not as backwards compatible, and places too emphasis on what the sentinel is rather than what is isn't.

-jJ



More information about the Python-Dev mailing list