Title: Add __contains__ to range type
Type: performance Stage:
Components: Interpreter Core Versions: Python 3.2
Status: closed Resolution: duplicate
Dependencies: Superseder: improve xrange.__contains__
View: 1766304
Assigned To: Nosy List: hpesoj, mark.dickinson
Priority: normal Keywords:

Created on 2009-09-02 20:49 by hpesoj, last changed 2009-09-02 21:09 by mark.dickinson. This issue is now closed.

File name Uploaded Description Edit hpesoj, 2009-09-02 20:49 Improved range demo script
Messages (2)
msg92183 - (view) Author: Joseph Thomson (hpesoj) Date: 2009-09-02 20:49
The range type should implement the __contains__ method.  Currently an
expression like 'x in range(10000000)' will iterate through every item
in the range (exiting when an item tests true for equality); this is
highly unnecessary as the following expression will perform the same
function in a fraction of the time:

value >= start and value < stop and (value - start) % step == 0

The biggest advantage of this modification would be to allow users to say:

if foo in range(lower, upper):

instead of:

if foo >= lower and foo < upper:

safe in the knowledge that they are losing no performance.  The former
is also far more Pythonic in my opinion :).

If there is still any doubt (which I doubt), I have attached an example
script showing what is to be gained.
msg92184 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-09-02 21:09
Closing this as a duplicate of issue 1766304.  Joseph, please could you 
renew the discussion in that issue instead?
Date User Action Args
2009-09-02 21:09:58mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg92184

superseder: improve xrange.__contains__
resolution: duplicate
2009-09-02 20:49:19hpesojcreate