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

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Jun 8 23:18:24 EDT 2009


On Tue, 09 Jun 2009 11:02:54 +1000, Ben Finney wrote:

> Carl Banks <pavlovevidence at gmail.com> writes:
> 
>> 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.
> 
> Yes, a set would be best for this specific situation.
> 
>> I don't think it's very important, however, to stick to rules like that
>> for objects that don't live for more than a single line of code.
> 
> It's important to the extent that it's important to express one's
> *meaning*. Program code should be written primarily as a means of
> communicating with other programmers, and only incidentally for the
> computer to execute.


But practicality beats purity -- there are many scenarios where we make 
compromises in our meaning in order to get correct, efficient code. E.g. 
we use floats, despite them being a poor substitute for the abstract Real 
numbers we mean.

In addition, using a tuple or a list in this context:

    if e.message.code in (25401,25402,25408):

is so idiomatic, that using a set in it's place would be distracting. 
Rather that efficiently communicating the programmer's intention, it 
would raise in my mind the question "that's strange, why are they using a 
set there instead of a tuple?".


-- 
Steven



More information about the Python-list mailing list