This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dilyan.palauzov
Recipients dilyan.palauzov
Date 2017-11-30.11:31:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1512041507.48.0.213398074469.issue32180@psf.upfronthosting.co.za>
In-reply-to
Content
Please make bool() on lists at least as fast as len() > 0 on lists is.

The trivial approach would be to define __bool__ on lists, that do something like  "True if self else False".

python3 
Python 3.6.3+ (heads/3.6-dirty:2b5cbbb13c, Nov  1 2017, 19:03:09) 
[GCC 6.4.1 20171025] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.timeit('len([]) > 0')
0.0983404889702797
>>> timeit.timeit('bool([])')
0.15502946823835373
>>> timeit.timeit('True if [] else False')
0.03108721226453781
>>> timeit.timeit('len([1]) > 0')
0.11656427383422852
>>> timeit.timeit('bool([1])')
0.19317257404327393
>>> timeit.timeit('True if [1] else False')
0.057590410113334656
History
Date User Action Args
2017-11-30 11:31:47dilyan.palauzovsetrecipients: + dilyan.palauzov
2017-11-30 11:31:47dilyan.palauzovsetmessageid: <1512041507.48.0.213398074469.issue32180@psf.upfronthosting.co.za>
2017-11-30 11:31:47dilyan.palauzovlinkissue32180 messages
2017-11-30 11:31:47dilyan.palauzovcreate