Author rhettinger
Recipients mark.dickinson, rhettinger, wyz23x2
Date 2021-02-02.13:25:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1612272333.44.0.766329665022.issue43097@roundup.psfhosted.org>
In-reply-to
Content
-1 on changing this because we would have to gum-up and slow down the code with an extra try-except to catch and reraise the exception with a different error message:

    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        try:
            return seq[self._randbelow(len(seq))]
        except IndexError:
            raise IndexError('Cannot choose from an empty sequence') from None

We rarely do that elsewhere in the code.  The norm in pure python code is that indexing into an empty list shows "IndexError: list index out of range" without further elaboration on what it means for the list to be empty.

FWIW, this behavior is very old and doesn't seem to have been a problem in practice.  Here is what the code looked like in Python 2.1:

    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        return seq[int(self.random() * len(seq))]
History
Date User Action Args
2021-02-02 13:25:33rhettingersetrecipients: + rhettinger, mark.dickinson, wyz23x2
2021-02-02 13:25:33rhettingersetmessageid: <1612272333.44.0.766329665022.issue43097@roundup.psfhosted.org>
2021-02-02 13:25:33rhettingerlinkissue43097 messages
2021-02-02 13:25:33rhettingercreate