diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2023-01-08 19:40:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-08 19:40:35 (GMT) |
commit | 9a68ff12c3e647a4f8dd935919ae296593770a6b (patch) | |
tree | ad74d7cc798d2ecd2bd87d7a09317d7875cac51a /Lib/random.py | |
parent | 87d3bd0e02cddc415a42573052110eb9301d2c3d (diff) | |
download | cpython-9a68ff12c3e647a4f8dd935919ae296593770a6b.zip cpython-9a68ff12c3e647a4f8dd935919ae296593770a6b.tar.gz cpython-9a68ff12c3e647a4f8dd935919ae296593770a6b.tar.bz2 |
GH-100805: Support numpy.array() in random.choice(). (GH-100830)
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/Lib/random.py b/Lib/random.py index e60b729..1c9e1a4 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -336,7 +336,10 @@ class Random(_random.Random): def choice(self, seq): """Choose a random element from a non-empty sequence.""" - if not seq: + + # As an accommodation for NumPy, we don't use "if not seq" + # because bool(numpy.array()) raises a ValueError. + if not len(seq): raise IndexError('Cannot choose from an empty sequence') return seq[self._randbelow(len(seq))] |