Other notes
Andrew Dalke
dalke at dalkescientific.com
Wed Dec 29 02:41:36 EST 2004
Paul Rubin wrote:
> ".." just becomes an operator like + or whatever, which you can define
> in your class definition:
>
> class MyClass:
> def __dotdot__(self, other):
> return xrange(self.whatsit(), other.whatsit())
>
> The .. operation is required to return an iterator.
Ahh, I see.
This should be put into a PEP. Some obvious questions:
- ".." or "..." ? The latter is already available for
use in slices
- If "..." should the name be "__ellipsis__"? If ".."
should the name be "__range___"?
- Should range(x, y) first attempt x.__range__(y)?
- Can it be used outside of a for statement? Ie, does
x = "a" ... "b"
return a generic iterator? Almost certainly as that
fits in nicely with the existing 'for' syntax.
- What's the precedence? Given
x = a .. b .. c
x = 1 + 2 .. 5 * 3
x = 1 ** 5 .. 4 ** 2
etc., what is meant? Most likely .. should have the
lowest precedence, evaluated left to right.
- is there an "__rdotdot__"?
- any way to specify "use the normal beginning"? Like
for x in .. 5: # same as 0 .. 5
-or (the oft rejected)-
for x in 5:
- any way to specify "ad infinitum"? Like
for x in 0 .. Infinity:
-or-
for x in 0 ... :
- does "for x in 10 .. 0" the same as xrange(10,0) (what
you propose) or xrange(10, 0, -1)?
- do strings work for len(s) > 1? Eg, "A000" .. "A999"?
- What do you think of (assuming the use of "...")
for x in 0.....100:?
- What's the advantage of ".." over, say a function or
method? That is, why does the new binary operator
prove so useful? In one of my gedanken experiments
I considered getting the 10th through 20th prime
for x in primes(10) .. primes(20):
but that seems clumsier than
for x in primes_between(10, 20):
in part because it requires "primes(10)" to have some
meaning. I suppose
for x in primes(10) .. 20:
could also work though that should in my mind generate
primes up to the number 20 and not the 20th prime.
Note that primes(10) cannot return the 10th prime as
the value 29.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list