more fun with PEP 276

phil hunt philh at comuno.freeserve.co.uk
Wed Dec 12 14:26:29 EST 2001


On Wed, 12 Dec 2001 09:12:48 -0800, David Eppstein <eppstein at ics.uci.edu> wrote:
>In article <slrna1f1le.4t5.philh at comuno.freeserve.co.uk>,
> philh at comuno.freeserve.co.uk (phil hunt) wrote:
>
>> how about:
>> 
>>    for i in -5 to 5:
>> 
>> or
>> 
>>    for i in -5 to 5 step 2:
>> 
>> 
>> Here, "to" and "to...step" are keywords that create an xrange with the
>> required values
>
>Not bad, but seems to force closed intervals only, which is somewhat at 
>odds with other features of Python.

Another possibility would be to make .. part of the syntax for lists, i.e.:

a = [ -5 .. 5, 10, 20 ]

a is [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 10, 20]

Of course, these features can be implemented in existing python, for
example using functions fromTo() and fromToStep() for the 1st usage,
and something like this for the second:

a = buildList(range(-5, 5), 10, 20)

etc.

IOW any new feature would be mere syntactic sugar that would have the
deficiency of making the compiler more complex.

Maybe instead we should be looking at implementing a clever macro system
as a preprocessor to Python. Something that could cope with ...to...step...
would have to be quite complex, but a macro system that used function-call
like syntax would be simpler.

-- 
*** Philip Hunt *** philh at comuno.freeserve.co.uk ***




More information about the Python-list mailing list