[ python-Bugs-964230 ] random.choice([]) should return more intelligible exception

SourceForge.net noreply at sourceforge.net
Sat Jun 5 05:24:04 EDT 2004


Bugs item #964230, was opened at 2004-06-01 12:39
Message generated for change (Comment added) made by hoffmanm
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=964230&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Open
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: Michael Hoffman (hoffmanm)
Date: 2004-06-05 09: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 06: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 06: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 05: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