preferring [] or () in list of error codes?
Chris Rebert
clp2 at rebertia.com
Mon Jun 8 23:06:19 EDT 2009
On Mon, Jun 8, 2009 at 6:57 PM, samwyse<samwyse at gmail.com> wrote:
> On Jun 8, 7:37 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
>> On Jun 8, 4:43 pm, Ben Finney <ben+pyt... at benfinney.id.au> wrote:
>> > m... at 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
Erm, unless I misunderstand you somehow, the second example will and
should *never* match the last.
The set {25401,25402,25408}, containing 3 integer elements, is quite
distinct from the set {(25401,25402,25408)}, containing one element
and that element is a tuple.
set(X) != {X}; set([X]) = {X}
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list