Skip to content

Conversation

@rhettinger
Copy link
Contributor

Besides improving the error message and using clear code instead of a comment, this makes shuffle(), sample(), and randrange() slightly faster.

rhettinger and others added 25 commits March 15, 2021 21:12
.
Merge branch 'master' of github.com:python/cpython
.
Merge branch 'master' of github.com:python/cpython
.
Merge branch 'master' of github.com:python/cpython
.
Merge branch 'master' of github.com:python/cpython
Merge branch 'main' of github.com:python/cpython into main
Merge branch 'main' of github.com:python/cpython
@rhettinger rhettinger added type-feature A feature request or enhancement performance Performance or resource usage skip issue skip news labels Dec 9, 2021
def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
# raises IndexError if seq is empty
if not seq:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhettinger : This change prevents operation on numpy arrays: if not seq will produce a ValueError (ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()). While it might be perverse to use random from stdlib on a numpy array, such code exists in the wild, such as in the test suite of scikit-learn which this change then breaks. Perhaps the test is better seq is None or len(seq) == 0, since that's what is actually needed for the next line about an empty sequence?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage skip issue skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants