[pycrypto] Issue with the new random.choice() unit test

Paul Koning paul_koning at dell.com
Wed Jan 5 13:14:58 CST 2011


On Jan 5, 2011, at 2:14 PM, Lorenz Quack wrote:

> Hey,
> 
> I just took a look at random.sample.
> The problem is that it uses a dict to simulate a set (introduced in py2.3).
> This is probably done because IIRC dict can do membership test in O(1).
> Lists on the other hand do membership test in O(n) so using sets/dict should
> provide a speedup if sample size k becomes large.
> So what's the problem? Membership testing for dicts was only added in py2.2.

True for "k in d" but "d.has_key(k)" works in 2.1.  Unfortunately, it doesn't work in Python 3.  But it would be easy enough to subclass dict and add that method to it in Python 3.

Alternatively, one could use a subscript operation on the dict, wrapped in try/except.

	paul



More information about the pycrypto mailing list