Re: Using 'in' with a Dict

2005-02-16 Thread Steven Bethard
kowboy wrote: I posted too quickly. A little performance testing told me that has_key is somewhat slower than "in". I used a large number of string keys in my test. See my other post, but the reason has_key is slower is almost certainly that it has to do a LOAD_ATTR: $ python -m timeit -s "m = di

Re: Using 'in' with a Dict

2005-02-16 Thread Fredrik Lundh
<[EMAIL PROTECTED]> wrote: >I was wondering if the following two "if" statements compile down to > the same bytecode for a standard Dictionary type: > > m = {"foo": 1, "blah": 2} > > if "foo" in m: > print "sweet" > > if m.has_key("foo"): > print "dude" nope. >>> import dis >>> dis.dis(compile

Re: Using 'in' with a Dict

2005-02-16 Thread Steven Bethard
[EMAIL PROTECTED] wrote: I was wondering if the following two "if" statements compile down to the same bytecode for a standard Dictionary type: m = {"foo": 1, "blah": 2} if "foo" in m: print "sweet" if m.has_key("foo"): print "dude" To answer the question you actually asked, you can use dis.dis

Re: Using 'in' with a Dict

2005-02-16 Thread Kartic
This is what I did >>> import compiler >>> exec1 = compiler.compile('''if "foo" in m: print "sweet"''', '', 'exec') >>> exec2 = compiler.compile('''if m.has_key("foo"): print "dude"''', '', 'exec') >>> exec1.co_code 'd\x01\x00e\x00\x00j\x06\x00o\t\x00\x01d\x02\x00GHn\x01\x00\x01d\x00\x00S'

Re: Using 'in' with a Dict

2005-02-16 Thread kowboy
I posted too quickly. A little performance testing told me that has_key is somewhat slower than "in". I used a large number of string keys in my test. -- http://mail.python.org/mailman/listinfo/python-list

Using 'in' with a Dict

2005-02-16 Thread cpmcdaniel
I was wondering if the following two "if" statements compile down to the same bytecode for a standard Dictionary type: m = {"foo": 1, "blah": 2} if "foo" in m: print "sweet" if m.has_key("foo"): print "dude" -- http://mail.python.org/mailman/listinfo/python-list