If Dict Contains a particular key

Alex Martelli aleax at mac.com
Thu Apr 26 22:24:56 EDT 2007


mensanator at aol.com <mensanator at aol.com> wrote:
   ...
> > > >      if 'a' in thedict:
   ...
> > > if thedict.has_key('a'):
   ...
> > Why consume twice as much time with no advantages whatever?!
> 
> Did I wander into comp.lang.perl by mistake?

Of course not -- note that the faster, modern idiom is also simpler,
more concise, _and_ more readable, _and_ also more general/polymorphic
than the tired old way that's still kept around only for backwards
compatibility.

Simplicity, terseness, readability, generality, _and_ speed, all at the
same time, as long as you use the "new" way (meaning, in this case, the
one that's been around for "only" about seven years...!!!) -- now that's
a combination of factors that's reasonably typical of Python, but not
quite so frequent in other languages!-)


Alex
 



More information about the Python-list mailing list