Skip to content

Commit dfb8144

Browse files
committed
Fold try/except into a conditional expression
1 parent 85ade39 commit dfb8144

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

Lib/random.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,7 @@ def sample(self, population, k, *, counts=None):
421421
cum_counts = list(_accumulate(counts))
422422
if len(cum_counts) != n:
423423
raise ValueError('The number of counts does not match the population')
424-
try:
425-
total = cum_counts.pop()
426-
except IndexError:
427-
total = 0
424+
total = cum_counts.pop() if cum_counts else 0
428425
if not isinstance(total, int):
429426
raise TypeError('Counts must be integers')
430427
if total < 0:

0 commit comments

Comments
 (0)