[PATCH] keyword: prefer i in d over d.has_key(i)

Christian Ebert blacktrash at gmx.net
Sun Jan 20 14:37:55 CST 2008


* Matt Mackall on Sunday, January 20, 2008 at 14:12:31 -0600
> Almost twice as fast as measured by timeit:
> 
> $ python -m timeit -s "a = {1:2}" "1 in a"
> 10000000 loops, best of 3: 0.187 usec per loop
> $ python -m timeit -s "a = {1:2}" "a.has_key(1)"
> 1000000 loops, best of 3: 0.352 usec per loop
> 
> Also, it's shorter, prettier and conveniently matches the syntax for
> lists.

So should these be changed:

$ grep -r --include='*.py' 'has_key(' mercurial/
./cmdutil.py:    if choice.has_key(cmd):
./dirstate.py:        if self._copymap.has_key(f):
./filelog.py:        if m and m.has_key("copy"):
./hgweb/hgweb_mod.py:        if req.env.has_key('REPO_NAME'):
./hgweb/hgweb_mod.py:            if req.form.has_key('style'):
./hgweb/hgweb_mod.py:        if req.form.has_key('style'):
./hgweb/hgweb_mod.py:        if req.form.has_key('node'):
./hgweb/hgweb_mod.py:        elif req.form.has_key('manifest'):
./hgweb/hgweb_mod.py:        if req.form.has_key('node'):
./hgweb/hgwebdir_mod.py:                if req.form.has_key('style'):
./hgweb/hgwebdir_mod.py:        if req.form.has_key('sort'):
./hgweb/hgwebdir_mod.py:        if req.form.has_key('style'):
./hgweb/protocol.py:    if req.form.has_key('nodes'):
./hgweb/protocol.py:    if req.form.has_key('pairs'):
./hgweb/protocol.py:    if req.form.has_key('roots'):
./hgweb/protocol.py:    if req.form.has_key('bases'):
./hgweb/protocol.py:    if req.form.has_key('heads'):
./hgweb/webcommands.py:    if req.form.has_key('file') and req.form['file'][0]:
./hgweb/webcommands.py:    if req.form.has_key('node'):
./hgweb/webcommands.py:        if req.form.has_key('rev'):
./httprepo.py:                if os.environ.has_key(env):
./keepalive.py:            if not self._hostmap.has_key(host): self._hostmap[host] = []
./keepalive.py:            if self._hostmap.has_key(host):
./localrepo.py:                if mf1.has_key(fn):
./localrepo.py:                if msng_filenode_set.has_key(fname):
./localrepo.py:                if msng_filenode_set.has_key(fname):
./templater.py:        if not self.cache.has_key(t):

And same for hgext?

c
-- 
Sometimes, a stick is just a stick.
--Sigmund Fraud


More information about the Mercurial-devel mailing list