tuples, index method, Python's design

Carsten Haese carsten at uniqsys.com
Tue Apr 10 11:47:05 EDT 2007


On Tue, 2007-04-10 at 11:44 -0400, Carsten Haese wrote:
> On Tue, 2007-04-10 at 17:10 +0200, BJörn Lindqvist wrote:
> > On 4/10/07, Carsten Haese <carsten at uniqsys.com> wrote:
> > > i = p.index(current_player)
> > > opponents = p[:i-1] + p[i+1:]
> > >
> > > An alternative is this:
> > >
> > > opponents = tuple(x for x in p if x is not current_player)
> > >
> > > You may disagree, but in my opinion, the alternative is better because
> > > it is a more natural translation of the concept that the opponents of
> > > the current player are all players that are not the 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.

_.replace("IndexError", "ValueError"), of course.

-Carsten





More information about the Python-list mailing list