wim glenn added the comment:
The implementation suggested by the OP
def choice(self, seq):
"""Choose a random element from a non-empty
sequence."""
idx = int(self.random() * len(seq))
try:
result = seq[idx] # raises IndexError if seq
is empty
except TypeError:
result = list(seq)[idx]
return result
Is broken because input may be a dict with, for example, keys 0 1 and 7 -
potentially causing the line result = seq[idx] to pass when logically it should
raise. Rather it would be needed to determine from the input whether it was a
non-sequence type collection, which sounds pretty hairy...
----------
nosy: +wim.glenn
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue1551113>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com