range() is not the best way to check range?

Paul Boddie paul at boddie.org.uk
Sun Jul 23 10:44:53 EDT 2006


Antoon Pardon wrote:
>
> Except that if you write your own class from scratch, you can't use
> it as a slice.

Correct, but we were actually discussing subclassing built-in classes
for use as a replacement for range/xrange. :-)

It may be "hard work" writing all those methods in a totally new
range/xrange class, but passing objects of that class around should
prove satisfactory for the use of most programs. I personally doubt
that it is that much hard work, especially if you stick to a reasonable
selection of list capabilities, for example, rather than attempting to
emulate support for every dodgy trick available to the programmer in
the modern CPython arsenal.

> For a language that is supposed to be about duck typing
> I find it strange that if I make my own class with a start, stop and
> step attribute, that python barfs on it when I want to use it as a
> slice.

Yes, my post showed this and gave a reference to where in the CPython
source code the tests for specific types are performed.

Paul




More information about the Python-list mailing list