[Python-ideas] A meaningful `if counter:`

cool-RR cool-rr at cool-rr.com
Tue Apr 5 23:16:57 CEST 2011


Hello folks,

I noticed today that bool(Counter({'a': 0'})) is True.

Is this wise? I want to be able to do:

    if my_counter:
        whatever

To check whether my counter has any elements. Currently this seems to be
impossible because of this behavior.

Will we have to keep this weird behavior because of backwards compatibility?
If so, perhaps `.elements` could be turned into a smart object so we could
at least do `if my_counter.elements():` and get the expected result.

To people who use `Counter`: Will you find the ability to do boolean checks
on a counter useful?

If you want a patch let me know and I'll write one.


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110405/e55178b6/attachment.html>


More information about the Python-ideas mailing list