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

Bruce Leban bruce at leapyear.org
Wed Apr 6 00:05:24 CEST 2011


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 at cool-rr.com> wrote:

> On Tue, Apr 5, 2011 at 11:34 PM, Bruce Leban <bruce at leapyear.org> wrote:
>
>> 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.
>>
>
> 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.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110405/1ee1ba97/attachment.html>


More information about the Python-ideas mailing list