[ python-Bugs-964230 ] random.choice([]) should return more
intelligible exception
SourceForge.net
noreply at sourceforge.net
Sat Jun 5 11:51:46 EDT 2004
Bugs item #964230, was opened at 2004-06-01 08:39
Message generated for change (Comment added) made by tjreedy
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=964230&group_id=5470
Category: Python Library
Group: Python 2.4
Status: Closed
Resolution: None
Priority: 2
Submitted By: Michael Hoffman (hoffmanm)
Assigned to: Nobody/Anonymous (nobody)
Summary: random.choice([]) should return more intelligible exception
Initial Comment:
Python 2.3.3 (#1, Mar 31 2004, 11:17:07)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> import random
>>> random.choice([])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/random.py", line 231, in choice
return seq[int(self.random() * len(seq))]
IndexError: list index out of range
This is simple enough here, but it's harder when it's
at the bottom of a traceback.
I suggest something like ValueError: sequence must not
be empty.
----------------------------------------------------------------------
Comment By: Terry J. Reedy (tjreedy)
Date: 2004-06-05 11:51
Message:
Logged In: YES
user_id=593130
More succinctly:
# empty seq raises IndexError
Commenting source code for traceback clarity has been
suggested before as a way to enhance without breaking.
Works for me
For making repeated choices, however, a generator would be
faster for n much larger than 0. Something like
def chooser(count, seq):
if type(count) not in (int, long) or count < 0:
raise ValueError('%s is not a count' % count)
try:
seqlen = len(seq)
if not seqlen: raise ValueError('Cannot choose from
nothing')
except TypeError:
raise ValueError('Seq arg must have length')
while count:
yield seq[int(self.random() * seqlen)]
count -= 1
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2004-06-05 10:54
Message:
Logged In: YES
user_id=80475
Okay. Fixed. See Lib/random.py 1.61.
----------------------------------------------------------------------
Comment By: Michael Hoffman (hoffmanm)
Date: 2004-06-05 05:24
Message:
Logged In: YES
user_id=987664
You have a point. What about rewriting the line to read:
return seq[int(self.random() * len(seq))] # raises IndexError if
seq is empty
The comment would be a good hint.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2004-06-05 02:41
Message:
Logged In: YES
user_id=80475
That's better, but I'm still -0. This function is apt to
be called inside a loop, so it would be a bummer to slow
down everyone's code just to rewrite the error message.
For better or worse, it is the nature of Python tracebacks
to raise RoadWeavingErrors when a DontDrinkAndDriveWarning
would be more to the point.
I recommend closing this, but if you really think it's an
essential life saver, then assign to Tim and see if you can
persuade him.
----------------------------------------------------------------------
Comment By: Michael Hoffman (hoffmanm)
Date: 2004-06-05 02:19
Message:
Logged In: YES
user_id=987664
I thought of that after I submitted. :-)
Might it be better to raise an IndexError with a message
similar to "sequence must not be empty" instead? It would
just make debugging that much easier.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2004-06-05 01:39
Message:
Logged In: YES
user_id=80475
-0
While a ValueError would be appropriate, the status quo
doesn't bug me much and changing it could break code if
someone is currently trapping the IndexError.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=964230&group_id=5470
More information about the Python-bugs-list
mailing list