[issue22446] Shortening code in abc.py

Serhiy Storchaka report at bugs.python.org
Fri Sep 19 22:57:15 CEST 2014


Serhiy Storchaka added the comment:

This is slower.

>>> import timeit
>>> class A(list):
...         def __contains__(self, value):
...             for v in self:
...                 if v == value:
...                     return True
...             return False
... 
>>> timeit.timeit('500 in x', setup='from __main__ import A; x = A(range(1000))', number=10000)
1.1222619999971357
>>> class B(list):
...         def __contains__(self, value):
...             return any(v == value for v in self)
... 
>>> timeit.timeit('500 in x', setup='from __main__ import B; x = B(range(1000))', number=10000)
2.05952100000286

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22446>
_______________________________________


More information about the Python-bugs-list mailing list