more fun with PEP 276

James_Althoff at i2.com James_Althoff at i2.com
Wed Dec 5 20:43:07 CET 2001


Jeremy Cromwell wrote:
>James,
<snip>
>Thanks to your hard work, we can play with this implementation and come up
>with some new ideas (maybe we'll even come up with a good one!)
>
>For example by adding different operators:
>  __lshift__, __rrshift__  = __div__, __rdiv__
>  __rshift__, __rlshift__  = __floordiv__, __rfloordiv__
>and setting:
>  __ = span # in the spirit of ...
>
>then
>
> >>> list(3>>__>>9)
> [4, 5, 6, 7, 8, 9]
> >>> list(3>>__<<9)
> [4, 5, 6, 7, 8]
> >>> list(3<<__<<9)
> [3, 4, 5, 6, 7, 8]
> >>> list(3<<__>>9)
> [3, 4, 5, 6, 7, 8, 9]
>
> which allows
>
>    for i in __<< table.rowcount:
>        for j in __<< table.colcount:
>            print table.value(i,j)
>

Thanks very much for the compliment and added ideas.

It would be nice if there were a way to use relational operators (or
something resembling them as you suggest with << and >>) instead of / and
//.  The magic method machinery doesn't seem to have quite enough power to
enable this.  But perhaps something could be done by way of enhancements to
make it possible.

Your suggestion of using "__" instead of span or "..." could work.  I don't
know how hard it would be to make "..." legal in a general expression (as
in -5 / ... / 5).  But "__" works out of the box. :-)

Jim





More information about the Python-list mailing list