[Python-ideas] Pass a function as the argument "step" of range()

Andrew Barnert abarnert at yahoo.com
Mon Jul 6 08:03:54 CEST 2015


On Jul 5, 2015, at 22:17, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
> On 6 July 2015 at 15:00, Andrew Barnert via Python-ideas
> <python-ideas at python.org> wrote:
>> At any rate, as I'm sure you know, that works in 1.5.2 because range returns
>> a list. Try it with range(1000000000) and you may not be quite as happy with
>> the result--but in 3.2+, it returns instantly, without using more than a few
>> dozen bytes of memory.
> 
> One kinda neat trick with Python 3 ranges is that you can actually
> work with computed ranges with a size that exceeds 2**64 (and hence
> can't be handled by len()):

I didn't realize that worked; nifty.

Also, I'm not sure why this message triggered this idea but...

The OP's example, or any geometric sequence, or anything that can be analytically integrated into an invertible function, can actually provide all the same features as range, without that much code. And that seems like a perfect example for demonstrating how to build a collections.abc.Sequence that's not like a tuple. Maybe a "collections cookbook" would be a useful thing to have in the HOWTO docs? (The OrderedSet recipe could also be moved there from ActiveState; I can't think of any other ideas that aren't overkill like a binary tree as a sorted Mapping or something.)




More information about the Python-ideas mailing list