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

Paul Boddie paul at boddie.org.uk
Thu Jul 20 11:25:23 EDT 2006


Alex Martelli wrote:
> Paul Boddie <paul at boddie.org.uk> wrote:
> >
> > Well, range is a function in the current implementation, although its
> > usage is similar to that one would get if it were a class, particularly
> > a subclass of list or one providing a list-style interface. With such a
> > class, you could provide a __contains__ method which could answer the
> > question of what the range contains based on the semantics guaranteed
> > by a range (in contrast to a normal list).
>
> You'd also have to override just about every mutating method to switch
> back to a "normal" __contains__ (or change self's type on the fly) -- a
> pretty heavy price to pay.

A subclass of list is probably a bad idea in hindsight, due to various
probable requirements of it actually needing to be a list with all its
contents, whereas we wanted to avoid having anything like a list around
until the contents of this "lazy list" were required by the program. If
we really wanted to subclass something, we could consider subclassing
the slice class/type, but that isn't subclassable in today's Python for
some reason, and it doesn't really provide anything substantial,
anyway. However, Python being the language it is, an appropriately
behaving class is quite easily written from scratch.

Paul




More information about the Python-list mailing list