PEP 276 (was Re: Status of PEP's?)

Emile van Sebille emile at fenx.com
Sat Mar 2 15:05:38 EST 2002


"Carel Fellinger"
> Emile van Sebille
> > "Carel Fellinger"
> >> You specify that the
> >> integer to iterate over should be possitive, why not allow for
> >> negative values and do the sensible thing, like:
> >>
> >>    >>> [i for i in -3]
> >>    [-1, -2, -3]
>
> > If you change the sensible thing to mean:
>
> >   [-3, -2, -1]
>
> > I'd like it better.  ;-)
>
> I noted the smiley, but just in case someone might think you have a
> point: what would be the benefit of that?

Syntactic consistency with slice notation:
    5 :: [:5] as-3 :: [-3:]

or maybe it should be:   ;-)
    5 :: [:5] like -3 :: [:-3]

>  It just let you loop over a
> sequence in the standard order, but now with negative indici.  And the
> range call to get this isn't that hard either: range(-3,0).
>
> My proposal makes iterating in *reverse* order simple.

On anything with a len.

> The tutorial could introduce it like:
>
>    In python integers are objects too. And objects know how to do
>    things, e.g. integer objects know how to add and just recently
>    they learned how to count.  Positive integers count from zero
>    upto but not including the number itself, whereas negative
>    integers count from -1 downto and including the number itself.

My my... I think you've converted... ;-)

>
>    A common place where this feature of integers is used is in for
>    loops and list comprehension. e.g.
>
>       >>> s = "silly walk".split()
>       >>> print " ".join([s[i] for i in len(s)])
>       silly walk
>       >>> print " ".join([s[i] for i in -len(s)])
>       walk silly
>
>    Although the first print statement is generally written as:
>
>       >>> print " ".join([x for x in s])

Couldn't we overload the '-' to reverse by itself?  ;-)
        >>> print " ".join([x for x in -s])


My point, repeated a few times here <wink>, is that while I have no
objections to (or say in) adding features in ways that allow for
pratical use, I'd prefer that there be syntactic consistency so that
each new feature doesn't add to the learning curve.  Arguments in favor
of producing 0,1,2 for 'for i in 3' have included that it's the same as
range(3), and that it maches slice notation, ala range(10)[:3].  For
negative numbers, it seems a natural extension for me to think in terms
of range(-3,0) and range(-10,0)[-3:].  Your reversing the sequence just
doesn't feel right.  ;-)  But, like >>, I'd get used to using it if it
were there.

Further, because there will always be different opinions of consistency,
a decision will ultimately be made in favor of one of the opposing
opinions.  The decision for (for i in 6), requires an iter to be added
to ints.  From a practical perspective, I don't see the harm in adding
it just because one group of people don't like iterating over integers.
We've simply provided an answer to 'What does it mean to iterate over an
integer?'

The three Pythonic opinions on this seem to be 'why not', 'an int's not
a collection and can't be iterated over', 'why bother', and 'find a
better way'.  I fall somewhere between the first and last of these.  I
illustrated with my sample dexing class the functionality I'd like to
see, although I won't be surprised to never see that in python.  For
those who don't like iter(int), don't use it, or at least, .  ;-) use it
as often as you use '>>'.

Bonus-points-for-each-point-found-here-ly y'rs,

--

Emile van Sebille
emile at fenx.com






More information about the Python-list mailing list