On Jun 8, 7:37 pm, Carl Banks <pavlovevide...@gmail.com> wrote: > On Jun 8, 4:43 pm, Ben Finney <ben+pyt...@benfinney.id.au> wrote: > > m...@pixar.com writes: > > > Is there any reason to prefer one or the other of these statements? > > > > if e.message.code in [25401,25402,25408]: > > > if e.message.code in (25401,25402,25408): > > If you want to go strictly by the book, I would say he ought to be > using a set since his collection of numbers has no meaningful order > nor does it make sense to list any item twice.
As the length of the list increases, the increased speeds of looking something up makes using a set makes more sense. But what's the best way to express this? Here are a few more comparisons (using Python 3.0)... >>> S=lambda x:x in set((25401,25402,25408)) >>> dis(S) 1 0 LOAD_FAST 0 (x) 3 LOAD_GLOBAL 0 (set) 6 LOAD_CONST 3 ((25401, 25402, 25408)) 9 CALL_FUNCTION 1 12 COMPARE_OP 6 (in) 15 RETURN_VALUE >>> S=lambda x:x in{25401,25402,25408} >>> dis(S) 1 0 LOAD_FAST 0 (x) 3 LOAD_CONST 0 (25401) 6 LOAD_CONST 1 (25402) 9 LOAD_CONST 2 (25408) 12 BUILD_SET 3 15 COMPARE_OP 6 (in) 18 RETURN_VALUE >>> S=lambda x:x in{(25401,25402,25408)} >>> dis(S) 1 0 LOAD_FAST 0 (x) 3 LOAD_CONST 3 ((25401, 25402, 25408)) 6 BUILD_SET 1 9 COMPARE_OP 6 (in) 12 RETURN_VALUE I conclude that using constructors is generally a bad idea, since the compiler doesn't know if you're calling the builtin or something with an overloaded name. I presume that the compiler will eventually optimize the second example to match the last, but both of them use the BUILD_SET opcode. I expect that this can be expensive for long lists, so I don't think that it's a good idea to use set constants inside loops. Instead it should be assigned to a global or class variable. -- http://mail.python.org/mailman/listinfo/python-list