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

Bruce Leban bruce at leapyear.org
Tue Apr 5 23:34:45 CEST 2011


According to the documentation
http://docs.python.org/release/3.1.3/library/collections.html#collections.Counter.elements
c.elements() returns an empty iterator if there are no elements with counts
> 0.

When you say you want it to be smart, what do you mean besides that?

--- Bruce
*New! *Puzzazz newsletter: http://j.mp/puzzazz-news-2011-04 including April
Fools!
*New!** *Blog post:
http://www.vroospeak.com/2011/04/march-gets-more-madness-next-year.html April
Fools!



On Tue, Apr 5, 2011 at 2:16 PM, cool-RR <cool-rr at cool-rr.com> wrote:

> 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.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110405/b84e76e8/attachment.html>


More information about the Python-ideas mailing list