[Python-ideas] Changing `Sequence.__contains__`

Ram Rachum ram.rachum at gmail.com
Mon Jul 21 00:06:33 CEST 2014


Why does the default `Sequence.__contains__` iterate through the items 
rather than use `.index`, which may sometimes be more efficient?

I suggest an implementation like this: 

    def __contains__(self, i):
        try: self.index(i)
        except ValueError: return False
        else: return True
        
What do you think? 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140720/571f95d5/attachment.html>


More information about the Python-ideas mailing list