This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author john.feuerstein
Recipients Julian, ezio.melotti, john.feuerstein, mark.dickinson, rhettinger
Date 2011-09-09.13:41:16
SpamBayes Score 1.8053557e-08
Marked as misclassified No
Message-id <1315575677.38.0.131111040893.issue12941@psf.upfronthosting.co.za>
In-reply-to
Content
> r.pop(random.randrange(0, len(r)))

So seq[random.randrange(0, len(seq))] suddenly makes random.choice(seq) obsolete? There is no functional reasoning here, it's convenience for a common operation.

> Not all the sequences have a .pop() method.

Not all sequences support indexing or item assignment either:

>>> random.choice({1, 2})
TypeError: 'set' object does not support indexing
>>> random.shuffle("abc")
TypeError: 'str' object does not support item assignment
...

> I'd rather see random.choice support sets: ...
> But that's another (rejected) issue; see issue 7522.

So the implicit requirement for random.choice() is support for indexing.
The implicit requirement for random.shuffle() is support for indexing and item assignment. The implicit requirement for random.pop() is support for seq.pop()...

I don't think random's convenience functions should validate (or even worse, convert) input. It's actually great that they are thin wrappers without magic, resulting in the same behaviour as if done without them.

> self.assertIn(pop([25, 75]), [25, 75])
> These should also verify that the element is not in the list anymore.
> Also other test with different (and possibly wrong) input types should be added.

This is true for many convenience functions in random. I've considered doing that at first and came to the conclusion that the tests for random should test the behaviour specific to random, not that of the underlying functionality?

So seq[random] should test for random behaviour, not that indexing of seq itself works correctly. Similarly, seq.pop(random) should test for random behaviour and not for a working seq.pop()?

All we would do here is duplicate tests not related to random.

Please correct me if I'm wrong. I'm glad that this started a discussion.
History
Date User Action Args
2011-09-09 13:41:17john.feuersteinsetrecipients: + john.feuerstein, rhettinger, mark.dickinson, ezio.melotti, Julian
2011-09-09 13:41:17john.feuersteinsetmessageid: <1315575677.38.0.131111040893.issue12941@psf.upfronthosting.co.za>
2011-09-09 13:41:16john.feuersteinlinkissue12941 messages
2011-09-09 13:41:16john.feuersteincreate