preferring [] or () in list of error codes?

Scott David Daniels Scott.Daniels at Acm.Org
Tue Jun 9 00:15:07 CEST 2009


mh at pixar.com wrote:
> 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):
> I'm currently using [], but only coz I think it's prettier
> than ().
> context: these are database errors and e is database exception,
> so there's probably been zillions of instructions and io's
> handling that already.

I lightly prefer the (a, b, c) -- you do put spaces after the comma,
don't you?  A tuple can be kept as a constant, but it requires (not
very heavy) program analysis to determine that the list need not be
constructed each time the statement is executed.  In addition, a
tuple is allocated as a single block, while a list is a pair of
allocations.

The cost is tiny, however, and your sense of aesthetics is part of
your code.  So unless you only very slightly prefer brackets, if I
were you I'd go with the list form.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list