[Python-Dev] Re: PEP239 (Rational Numbers) Reference Implemen
tation and new issues
Delaney, Timothy
tdelaney@avaya.com
Fri, 4 Oct 2002 11:50:55 +1000
> From: Greg Ewing [mailto:greg@cosc.canterbury.ac.nz]
>
> > I think the slice notation e.g. x[1:4] kills that idea.
>
> Maybe if ":" were a sliceobject-creating operator usable
> anwyere, not just in [...], and you added arithmetic
> methods to sliceobjects so you could use them as
> rationals...
The sad thing is this is actually tempting to me, given the beauty of using
the "ratio" notation ``x:y`` to denote rational literals ...
Actually, following through my thought processes, ``x:y`` is only legal in
the context of a sequence slice i.e. inside ``[]``. Therefore ``x:y``
*could* be used outside of a sequence context for rationals. There would be
no ambiguity if rationals could not be used in such a context, just as
floats can't be (which would be consistent).
e.g.
``a[1:2]`` is *always* a slice.
``b:2`` is always a rational.
The problem of course is that the ``:`` operator then becomes overloaded
depending on its context. Not a good thing.
Damn. Just realised of course that a rational could be a dictionary index.
Perhaps in that case slice takes precedence, meaning that to use a rational
expression as an index it would need to be disambiguated as:
``d[(1:2)]``
This boils down to 2 rules:
1. In an indexing context, ``:`` denotes a slice expression;
2. In a non-indexing context, ``:`` denotes a rational expression.
with the corollary:
1. In an indexing context, a rational expression must be disambiguated using
parentheses.
Tim Delaney