[Python-Dev] if key in dict?

Ka-Ping Yee ping@lfw.org
Thu, 13 Apr 2000 04:50:02 -0700 (PDT)


On Thu, 13 Apr 2000, Greg Stein wrote:
> On Thu, 13 Apr 2000, Fredrik Lundh wrote:
> > now that we have the sq_contains slot, would it make
> > sense to add support for "key in dict" ?
> > 
> > after all,
> > 
> >     if key in dict:
> >         ...
> 
> The counter has always been, "but couldn't that be read as 'if value in
> dict' ??"

I've been quite happy with "if key in dict".  I forget if i already
made this analogy when it came up in regard to the issue of supporting
a "set" type, but if you think of it like a real dictionary -- when
someone asks you if a particular word is "in the dictionary", you look
it up in the keys of the dictionary, not in the definitions.

And it does read much better than has_key, and makes it easier to use
dicts like sets.

So i think it would be nice, though i've seen this meet opposition before.


-- ?!ng

"You should either succeed gloriously or fail miserably.  Just getting
by is the worst thing you can do."
    -- Larry Smith