Arithmetic sequences in Python

Paul Rubin http
Mon Jan 16 20:40:43 EST 2006


Tom Anderson <twic at urchin.earth.li> writes:
> The natural way to implement this would be to make .. a normal
> operator, rather than magic, and add a __range__ special method to
> handle it. "a .. b" would translate to "a.__range__(b)". I note that
> Roman Suzi proposed this back in 2001, after PEP 204 was
> rejected. It's a pretty obvious implementation, after all.

Interesting, but what do you do about the "unary postfix" (1 ..)
infinite generator?

> > (-3,-5 ..)   -->  'infinite' generator that yield -3,-5,-7 and so on
> 
> -1. Personally, i find the approach of specifying the first two
> elements *absolutely* *revolting*, and it would consistently be more
> awkward to use than a start/step/stop style syntax. Come on, when do
> you know the first two terms but not the step size?

Usually you know both, but showing the first two elements makes
sequence more visible.  I certainly like (1,3..9) better than (1,9;2)
or whatever.

> > 1) "[]" means list, "()" means generator
> Yuck. Yes, i know it's consistent with list comps and genexps, but
> yuck to those too!

I'd be ok with getting rid of [] and just having generators or
xrange-like class instances.  If you want to coerce one of those to a
list, you'd say list((1..5)) instead of [1..5].



More information about the Python-list mailing list