On Sat, 21 Jul 2007 09:22:32 +0530, Rustom Mody wrote > Can someone who knows about python internals throw some light on why > >>> x in dic > is cheaper than > >>> dic.has_key(x) > > ??
I won't claim to know Python internals, but compiling and disassembling the expressions in question reveals the reason: >>> from compiler import compile >>> from dis import dis >>> dis(compile("dic.has_key(x)","","eval")) 1 0 LOAD_NAME 0 (dic) 3 LOAD_ATTR 1 (has_key) 6 LOAD_NAME 2 (x) 9 CALL_FUNCTION 1 12 RETURN_VALUE >>> dis(compile("x in dic","","eval")) 1 0 LOAD_NAME 0 (x) 3 LOAD_NAME 1 (dic) 6 COMPARE_OP 6 (in) 9 RETURN_VALUE "dic.has_key(x)" goes through an attribute lookup to find the function that looks for the key. "x in dic" finds the function more directly. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list