[issue1551113] random.choice(setinstance) fails
Raymond Hettinger added the comment: Sorry, this is still rejected for the reasons that Tim mentioned. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1551113 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1551113] random.choice(setinstance) fails
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 rep...@bugs.python.org http://bugs.python.org/issue1551113 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1551113] random.choice(setinstance) fails
wim glenn added the comment: How about if isinstance(seq, collections.Sequence): # do it the usual way .. else: return choice(list(seq)) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1551113 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1551113] random.choice(setinstance) fails
Tim Peters tim.pet...@gmail.com added the comment: The CPython set/dict implementation does not guarantee minimal constant density, so quite easy doesn't apply in reality. For example, a set that once contained a million elements may still contain a million /slots/ for elements after all but one of the elements has been removed. And in that case, the only way to find the sole occupied slot is to search over the million allocated slots. In short, all the comments in this item from 2006 still apply without change. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1551113 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1551113] random.choice(setinstance) fails
Tim Peters tim.pet...@gmail.com added the comment: The CPython set/dict implementation does not guarantee minimal constant density, so quite easy doesn't apply in reality. For example, a set that once contained a million elements may still contain a million /slots/ for elements after all but one of the elements has been removed. And in that case, the only way to find the sole occupied slot is to search over the million allocated slots. In short, all the comments in this item from 2006 still apply without change. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1551113 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1551113] random.choice(setinstance) fails
Changes by Tim Peters tim.pet...@gmail.com: -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1551113 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com