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

Grant Edwards grante at visi.com
Tue Jul 18 13:56:47 EDT 2006


On 2006-07-18, Paul Boddie <paul at boddie.org.uk> wrote:

>> It's unclear what you're referring to as "the range".
>
> The notion of something describing a range of values which can be
> expanded to a list or, of relevance here, whose boundaries can be
> tested efficiently.
>
>> Perhaps you're thinking of a slice?  Somethign like
>>
>>   if  (0:10000).contains(x):

I didn't mean to imply that would actually work, but I thought
maybe that's what you were proposing.

> Did you mean...?
>
> (0:10000) # SyntaxError
> slice(0, 10000).contains(x) # AttributeError
> 3 in slice(0, 10000) # TypeError
>
> Something like this might suffice if slice could have a __contains__
> method or if people thought of slices as natural things to test
> against.

A slice seems to me to be the obvious way to represent a finite
length algebraic sequence of integers.  However, obvioiusness
is in the eye of the beholder since as you point out below to
the OP, a range() was the obvious way to it.

> Perhaps we could ask the original questioner why they chose to
> use range in such a way - it might indicate a background in
> languages which encourage the construction of ranges and their
> use in comparisons - although being told to RTFM may have
> scared them off.

-- 
Grant Edwards                   grante             Yow!  Did I say I was a
                                  at               sardine? Or a bus???
                               visi.com            



More information about the Python-list mailing list