[Python-Dev] redefining is

Barry Warsaw barry at python.org
Thu Mar 18 13:47:01 EST 2004


On Thu, 2004-03-18 at 13:39, Andrew Koenig wrote:
> > A very common use case in Python is where None is a valid value in a
> > dictionary:
> > 
> > missing = object()
> > 
> > if d.get('somekey', missing) is missing:
> >    # it ain't there
> > 
> > It even reads well!
> 
> Indeed.  Of course, object() is mutable, so there is no proposal to change
> the meaning of this program.  What I'm concerned about is someone trying to
> do the same thing this way:
> 
> 	missing = 'missing'
> 
> 	if d.get('somekey', missing) is 'missing':
> 		# it ain't there
> 
> This code contains a bug, but on an implementation that interns strings that
> happen to look like identifiers, no test will detect the bug.

Sure, but it's long been recommended against doing stuff like:

try:
  foo()
except 'quit':
  pass

def foo():
  raise 'quit'

This doesn't seem like it's something broken that needs fixing.  Once
you understand what's going on, it makes sense.

-Barry





More information about the Python-Dev mailing list