Arithmetic sequences in Python
Tom Anderson
twic at urchin.earth.li
Tue Jan 17 18:57:03 EST 2006
On Tue, 16 Jan 2006, it was written:
> 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?
1.__range__(None)
>>> (-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.
I have to confess that i don't have a pretty three-argument syntax to
offer as an alternative to yours. But i'm afraid i still don't like yours.
:)
>>> 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].
Sounds good. More generally, i'd be more than happy to get rid of list
comprehensions, letting people use list(genexp) instead. That would
obviously be a Py3k thing, though.
tom
--
Taking care of business
More information about the Python-list
mailing list