Skip to content

Commit

Permalink
Fold try/except into a conditional expression
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed Feb 19, 2025
1 parent 85ade39 commit dfb8144
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,7 @@ def sample(self, population, k, *, counts=None):
cum_counts = list(_accumulate(counts))
if len(cum_counts) != n:
raise ValueError('The number of counts does not match the population')
try:
total = cum_counts.pop()
except IndexError:
total = 0
total = cum_counts.pop() if cum_counts else 0
if not isinstance(total, int):
raise TypeError('Counts must be integers')
if total < 0:
Expand Down

0 comments on commit dfb8144

Please sign in to comment.