range() is not the best way to check range?
John Machin
sjmachin at lexicon.net
Tue Jul 18 17:45:22 EDT 2006
On 19/07/2006 1:05 AM, Dan Bishop wrote:
> Paul Boddie wrote:
>
>> Yes, he wants range to return an iterator, just like xrange more or
>> less does now. Given that xrange objects support __getitem__, unlike a
>> lot of other iterators (and, of course, generators), adding
>> __contains__ wouldn't be much of a hardship. Certainly, compared to
>> other notational conveniences bounced around on the various development
>> lists, this one would probably provide an order of magnitude
>> improvement on the usual bang per buck development ratio.
>
> xrange already has __contains__.
As pointed out previously, xrange is a function and one would not expect
it to have a __contains__ method.
The objects returned by xrange do not (according to my reading of the
2.4.3 version of Objects/rangeobject.c) have a __contains__ method.
I find it difficult to believe that an inefficient __contains__ has been
implemented since.
Perhaps you are unaware that the mere fact that an object supports the
"in" operation does not mean that this support is provided by a
__contains__ method. The following section of the manual may help:
"""
The membership test operators (in and not in) are normally implemented
as an iteration through a sequence. However, container objects can
supply the following special method with a more efficient
implementation, which also does not require the object be a sequence.
__contains__( self, item)
Called to implement membership test operators. Should return true
if item is in self, false otherwise. For mapping objects, this should
consider the keys of the mapping rather than the values or the key-item
pairs.
"""
More information about the Python-list
mailing list