> >>> for x in xrange(2**31): > pass > > Traceback (most recent call last): > File "<pyshell#16>", line 1, in <module> > for x in xrange(2**31): > OverflowError: long int too large to convert to int
Hi Dick, Hmmm... I'd consider this a misfeature in the implementation. Apparently xrange (and range) must be built with regular ints, not long ints. This limitation is documented, although I think this is inconsistent and a bad thing. http://www.python.org/doc/2.3.5/lib/built-in-funcs.html#l2h-73 http://www.python.org/doc/2.3.5/lib/built-in-funcs.html#l2h-55 In the meantime, you can build your own version of xrange that should work on long ints. Here is an example: ################# def myxrange(m, n=None, skip=1): """An xrange-like function that can deal with long ints. Note: doesn't deal with negative ranges particularly well.""" if n is None: m, n = 0, m i = m while i < n: yield i i = i + skip ################# This is a "generator" that does pretty much what you'll want. ################################################################## >>> list(myxrange(1, 5)) [1, 2, 3, 4] >>> list(myxrange(2**32, 2**32 + 5)) [4294967296L, 4294967297L, 4294967298L, 4294967299L, 4294967300L] ################################################################## Note: this is a quick-and-dirty function; I wouldn't use it in production yet. It needs some more work to reject bad input. If you want, we can talk about exhaustively testing the function using robust equivalence class testing. *grin* Best of wishes! _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor