tuples, index method, Python's design

BJörn Lindqvist bjourne at gmail.com
Tue Apr 10 19:21:37 CEST 2007


On 4/10/07, Carsten Haese <carsten at uniqsys.com> wrote:
> > > opponents = tuple(x for x in p if x is not current_player)
> > >
> > Your alternative is wrong because it wont raise ValueError if
> > current_player is not present in the tuple. Please revise your
> > "solution."
>
> You have a point. Here is my revised solution:
>
> assert current_player in p
> opponents = tuple(x for x in p if x is not current_player)
>
> The added advantage is that AssertionError is better than IndexError for
> conveying that a severe program bug has occurred.

Assertions are not checked when you run scripts with -O. Furthermore,
changing the exception type and the message it produces, is quite a
big deviation from list.index.

-- 
mvh Björn



More information about the Python-list mailing list