You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
random.sample() docs unclear on k < len(population) #59470
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/rhettinger'closed_at=<Date2012-07-08.23:02:27.161>created_at=<Date2012-07-06.17:50:44.677>labels= ['type-feature', 'docs']
title='random.sample() docs unclear on k < len(population)'updated_at=<Date2012-07-08.23:02:27.160>user='https://bugs.python.org/roysmith'
The docs don't say what happens if you call random.sample() with a population smaller than k. Experimentally, it raises ValueError, but this should be documented.
I would have guessed it would return IndexError, by analogy to random.choice().
Activity
roysmith commentedon Jul 6, 2012
The docs don't say what happens if you call random.sample() with a population smaller than k. Experimentally, it raises ValueError, but this should be documented.
I would have guessed it would return IndexError, by analogy to random.choice().
kmox83 commentedon Jul 7, 2012
Added the related line to the documentation describing when the exception is raised.
roysmith commentedon Jul 7, 2012
The docs describe population as a "sequence". Your patch describes it as a "list". I would go with:
If *len(population)* is less than *k*, raises :exc:`ValueError`.
kmox83 commentedon Jul 7, 2012
Ok, I have updated the patch
python-dev commentedon Jul 8, 2012
New changeset 72174d8af3ba by Raymond Hettinger in branch 'default':
bpo-15265: document the exception raised for invalid sample sizes.
http://hg.python.org/cpython/rev/72174d8af3ba
rhettinger commentedon Jul 8, 2012
Thanks, I've added a note.