
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.

According to the documentation http://docs.python.org/release/3.1.3/library/collections.html#collections.Co... 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@cool-rr.com> wrote:

On Tue, Apr 5, 2011 at 11:34 PM, Bruce Leban <bruce@leapyear.org> wrote:
I am aware of the current behavior.
When you say you want it to be smart, what do you mean besides that?
I mean that it will be like `dict.keys`; an object which behaves similarly to a list but is implemented more smartly. e.g., `len(counter.elements())` would be implemented as `sum(counter.values())` for better O complexity, and `counter.elements().__bool__` would be (trivially) implemented. (You currently can't do a meaningful `if counter.elements()`). Ram.

You're mixing implementation details with behavior. I agree it would be nice if bool(counter.elements()) was False if the list of elements is empty. Supporting len(counter.elements()) could be done if it was useful, but I'm not sure what the use case is. --- 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:46 PM, cool-RR <cool-rr@cool-rr.com> wrote:

On Apr 5, 2011, at 2:16 PM, cool-RR wrote:
As explained on tracker where you made the same request, that counter is not an empty container. Accordingly, it's bool value is True. The python language has been consistent with this for a long time (as I found-out years ago when I proposed putting a __len__ attribute on various iterools when the output iterator length was known, but Guido wanted the iterator to always have a bool value of False). A Counter object is essentially just a dictionary with a __missing__ method to supply an implied zero default count. Accordingly, it needs to behave as much like a dictionary as possible. Raymond

On Wed, Apr 6, 2011 at 1:54 AM, Raymond Hettinger < raymond.hettinger@gmail.com> wrote:
Okay, this argument applies against `Counter.__bool__`, but not against `Counter.elements().__bool__`. So I want to hear whether people would find a `Counter.elements().__bool__` method useful. Ram.

No it won't.
--- 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 Wed, Apr 6, 2011 at 1:00 AM, cool-RR <cool-rr@cool-rr.com> wrote:

Nope.
--- 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 Wed, Apr 6, 2011 at 7:02 AM, cool-RR <cool-rr@cool-rr.com> wrote:

According to the documentation http://docs.python.org/release/3.1.3/library/collections.html#collections.Co... 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@cool-rr.com> wrote:

On Tue, Apr 5, 2011 at 11:34 PM, Bruce Leban <bruce@leapyear.org> wrote:
I am aware of the current behavior.
When you say you want it to be smart, what do you mean besides that?
I mean that it will be like `dict.keys`; an object which behaves similarly to a list but is implemented more smartly. e.g., `len(counter.elements())` would be implemented as `sum(counter.values())` for better O complexity, and `counter.elements().__bool__` would be (trivially) implemented. (You currently can't do a meaningful `if counter.elements()`). Ram.

You're mixing implementation details with behavior. I agree it would be nice if bool(counter.elements()) was False if the list of elements is empty. Supporting len(counter.elements()) could be done if it was useful, but I'm not sure what the use case is. --- 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:46 PM, cool-RR <cool-rr@cool-rr.com> wrote:

On Apr 5, 2011, at 2:16 PM, cool-RR wrote:
As explained on tracker where you made the same request, that counter is not an empty container. Accordingly, it's bool value is True. The python language has been consistent with this for a long time (as I found-out years ago when I proposed putting a __len__ attribute on various iterools when the output iterator length was known, but Guido wanted the iterator to always have a bool value of False). A Counter object is essentially just a dictionary with a __missing__ method to supply an implied zero default count. Accordingly, it needs to behave as much like a dictionary as possible. Raymond

On Wed, Apr 6, 2011 at 1:54 AM, Raymond Hettinger < raymond.hettinger@gmail.com> wrote:
Okay, this argument applies against `Counter.__bool__`, but not against `Counter.elements().__bool__`. So I want to hear whether people would find a `Counter.elements().__bool__` method useful. Ram.

On Apr 5, 2011, at 11:59 PM, cool-RR wrote:
Okay, this argument applies against `Counter.__bool__`, but not against `Counter.elements().__bool__`. So I want to hear whether people would find a `Counter.elements().__bool__` method useful.
if any(c.elements()): ... Raymond

No it won't.
--- 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 Wed, Apr 6, 2011 at 1:00 AM, cool-RR <cool-rr@cool-rr.com> wrote:

Nope.
--- 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 Wed, Apr 6, 2011 at 7:02 AM, cool-RR <cool-rr@cool-rr.com> wrote:
participants (4)
-
Bruce Leban
-
cool-RR
-
MRAB
-
Raymond Hettinger